I have a resource route in my application and I think the update method is never called because theres the show method that has the same URL (/leads/{id}). Even though I have the form method set to PUT on the update form it's still using the show method.
Please help
Show method of LeadsController:
public function show($id)
{
$lead = Lead::findOrFail($id);
return view('leads.lead',compact('lead'));
}
Update method of LeadsController:
public function update(UpdateLeadRequest $request, $id)
{
$lead = Lead::find($id);
$tmp = ['email'=>'UPDATED'];
$lead->update($tmp);
return view('leads.lead',compact('lead'));
}
Form to update leads:
<form action="{{action('LeadsController@update',['id'=>$lead->id])}}" method="put">
<input type="hidden" name="_token" value="{{csrf_token()}}" />
<input type="hidden" name="_method" value="put" />
<div class="input-group">
<label for="email">Email:</label>
<input type="email" name="email" placeholder="Email" class="form-control" value="{{$lead->email}}" />
</div><br />
<input type="submit" class="btn btn-primary" value="Save"/>
</form>
Thanks mirbatworks but I figured it out, for future reference my form should have been like this:
<form action="{{action('LeadsController@update',['id'=>$lead->id])}}" method="post">
<input type="hidden" name="_method" value="put" />
method put is no valid for forms, instead I have to use POST and send a hidden field named _method with the value put
Thanks anyway
If you have in controller route with { id } Something like this:
Route::get('leads/{id}', 'LeadsController@show');
Route::get('leads/update', 'LeadsController@update');
Your controller will always load show method even if your url is leads/update because of your {id}
In that case you need to change sequences
//first:
Route::get('leads/update', 'LeadsController@update');
// than:
Route::get('leads/{id}', 'LeadsController@show');
i am using a resource route so that does not apply in my case
Route::resource('LeadsController');
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community