Delete function not working with controller - laravel

Hello guy's my I just wrote an delete for my project, it lookes like that:
public function projectdelete(Project $project)
{
$project->delete();
return back();
}
My form :
<form action="{{route('project.delete',$project )}}"
method="POST"
onsubmit="return confirm('Are you sure you want to delete the project?')">
#csrf
#method('DELETE')
<button type="submit">Submit</button>
</form>
Route:
Route::delete('/dashboard/project/create/{id}', [
DashboardController::class, 'projectdelete'
])->name('project.delete');
Why is not deleting it?

The problem is on the parameter, your route parameter is {id}, on controller you are using Model Binding, for model binding your route parameter name need to be matched with your model {project} :
Route::delete('/dashboard/project/create/{project}', [
DashboardController::class, 'projectdelete'
])->name('project.delete');

In your blade file pass the project parameter as project and also in your routes give your parameter name as project:
My form :
<form action="{{route('project.delete', ['project' => $project])}}" method="POST" onsubmit="return confirm('Are you sure you want to delete the project?')">
#csrf
#method('DELETE')
<button type="submit">Submit</button>
</form>
Route:
Route::delete('/dashboard/project/create/{project}', [DashboardController::class, 'projectdelete'])->name('project.delete');

Related

Solution for redirect to POST route Laravel

SCENARIO:
When I tried to search (POST route) a keyword, results of item will be display as lists. These items has a button (Save or UnSave) on each list. The save button, is either form POST or DELETE method and what happens is when this button clicked, either the item will SAVE or UNSAVE from the tables.
ERROR
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: POST.
ROUTES:
Route::post('/jobseeker/jobs/search-results', [JobSeekerController::class, 'searchJobsNow'])->name('jobseeker.jobs.searchNow');
Route::post('/jobseeker/saved-jobs/{id}/save', [JobSeekerController::class, 'saveJob'])->name('jobseeker.savedJobs.save');
Route::delete('/jobseeker/saved-jobs/{id}/unsave', [JobSeekerController::class, 'unsaveJob'])->name('jobseeker.savedJobs.unsave');
CONTROLLER:
public function searchJobsNow(Request $request)
{
// codes
}
public function saveJob()
{
// code for saving a job
return redirect()->back();
}
public function unsaveJob()
{
// code for unsaving a job
return redirect()->back();
}
VIEW
#if ($job->candidateHadSavedTheJob)
<form action="{{ route('jobseeker.savedJobs.unsave', $job->id) }}" method="POST">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-primary">Saved</button>
</form>
#else
<form action="{{ route('jobseeker.savedJobs.save', $job->id) }}" method="POST">
#csrf
<button type="submit" class="btn btn-outline-primary"> Save</button>
</form>
#endif

Error message "The GET method is not supported for this route. Supported methods: POST." in laravel 8

Following is my controller. Method ProjectsView is to view all the listings. The second method BackendProjectSearch is for searching of projects. The first page of search result is displayed properly, but when we click on next page it gives the error "The GET method is not supported for this route. Supported methods: POST."
What should I do ?
public function ProjectsView(){
$projects = projects::orderBy('id','ASC')->paginate(15);
return view('backend.projects.projects_view',compact('projects')); }
public function BackendProjectSearch(Request $request){
$request->validate(["search" => "required"]);
$item = $request->search;
$projects = Projects::where('project_name','LIKE',"%$item%")->paginate(15);
return view('backend.projects.projects_view',compact('projects')); }
Following are the routes for both the methods :
Route::post('/backend/project/search', [ProjectsController::class, 'BackendProjectSearch'])->name('backend.project.search');
Route::get('/view', [ProjectsController::class, 'projectsView'])->name('projects.view');
View code :
<div class="col-md-8">
<div class="header-navsearch">
<form class="form-inline mr-auto" method="post" action="{{route('backend.project.search')}}">
#csrf
<div class="nav-search">
<input type="search" name="search" class="form-control header-search" placeholder="Search projects…" aria-label="Search">
<button class="btn btn-primary" type="submit"><i class="fa fa-search"></i></button>
</div>
</form>
</div>
</div>
The route for this request should be post, not get.
Example
Route::post(-----);
Also, make sure to insert a CSRF token while making the request
it means you now cannot do something like the following.
Instead, you have to do something like...
<form action="{{ route('example') }}" method="POST">
#csrf {{-- According to the version of laravel --}}
{{-- Your other codes --}}
<button type="submit" class="">Submit</button>
</form>
You have to use the below code
return redirect()->route('projects.view')->with(['projects' => $projects]);
Your route might be little change
Route::get('/view/{projects?}', [ProjectsController::class, 'projectsView'])->name('projects.view');
and In your controller, you have to change function like this
public function ProjectsView($projects = null){
if($projects!=null){
return view('backend.projects.projects_view',compact('projects'));
}
else{
$projects = projects::orderBy('id','ASC')->paginate(15);
return view('backend.projects.projects_view',compact('projects'));
}

laravel form is not submitted

I am new to laravel. This is my view (bids.new):
<form method="post" action="{{ route('bids.storeBid', $project->id) }}" enctype="multipart/form-data" >
#csrf
*content*
<div class="row pt-4">
<button class="btn btn-primary">Place Bid</button>
</div>
</form>
This is my route:
Route::get('/bids/new/{id}', 'BidController#create')->name('bids.new');
Route::post('/bids/{id}', 'BidController#storeBid')->name('bids.storeBid');
BidController:
public function create($id)
{
$project = Projects::findOrFail($id);
return view('bids.new',compact('project'));
}
public function storeBid(Request $request, $id)
{
*content*
}
When I clicked on Place Bid button in my view, the page is not responding and URL is still showing /bids/new/1 which means the storeBid route was not loaded. I tried using dd($id) at controller but it is not displaying as well so I assumed I have a problem with the route or form in the view.
<div class="row pt-4">
<button type="submit" class="btn btn-primary">Place Bid</button>
</div>
try this one. I hope it will help you. Let me know.
Thank you guys for the comments, I figured out the problem was not because of the form or route but due to some errors on my eloquent in the controllers.

laravel 5.5 | old() empty in view unless $request->flash() used

I've run into an odd issue where the helper function old() always returns null in a blade view unless $request->flash() is used prior to loading the view. I have never had to do this when using laravel in the past. Did something change or is there something that I have forgotten to set/configure. Below is a simple example of the behavior:
web.php
Route::get('/test', function(){
return view('testView');
});
Route::post('/test', function(Illuminate\Http\Request $request){
$request->flash(); // if uncommented old() works, if commented old() does not work
return view('testView');
});
form in testView.blade.php
<form action="/test" method="POST">
{{csrf_field()}}
<input type="hidden" name="test001" value="001"/>
<input type="hidden" name="test002" value="002"/>
<div class="">
{{old('test001')}}
<br/>
{{old('test002')}}
</div>
<button type="submit">GO</button>
</form>
after form submitted without $request->flash()
after form submitted with $request->flash()
EDIT
Thinking this might have something to do with using a single route name for both post and get methods, the form was changed so to submit via get, and the issue persists. For example:
web.php
Route::get('/test', function(function(Illuminate\Http\Request $request){
return view('testView');
});
form in testView.blade.php
<form action="/test" method="GET">
<input type="hidden" name="test001" value="001"/>
<input type="hidden" name="test002" value="002"/>
<div class="">
{{old('test001')}}
<br/>
{{old('test002')}}
</div>
<button type="submit">GO</button>
</form>
Use redirect back() instead of loading view directly in a post method.
return redirect()->back()->withInput();
You need to flash request data to put old input into session, otherwise old() will return empty result. See official doc here.

Laravel 5 Plain Html MethodNotAllowedHttpException

Hello so I am trying to make an update form in Laravel using plain html. The url of my edit form is http://localhost:8000/profile/sorxrob/edit, sorxrob is the username. And the code in that url is:
<form class="form-horizontal" role="form" action="" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input name="_method" type="hidden" value="PATCH">
//more inputs
</form>
And in my controller named AccountController:
public function update(Request $request, $id)
{
$account = Accounts::findOrFail($id);
$input = $request->all();
$account->fill($input)->save();
return 'success';
}
And I am getting the error MethodNotAllowedHttpException when I click the update button. Is it because my action is equal to nothing? If yes, what is the correct way of routing there?
This is due to your action url which is not correct routing url. Use the following
(1) First define a route in your route.php file
Route::post('profile/{username}/edit', array('as' => 'profile.update', 'uses' => 'AccountController#update'));
(2) Change your action attribute from your form tag
action="{{ URL::route('profile.update', [$username]) }}"
here $username variable will be passed from your AccountController#edit method.

Resources