Reputation: 91
I have a resource controller for member data. All of the usual resource functions, including edit, are working perfectly. I am trying to add additional edit functions within this controller so that I can create views that only are for specific subsets of the Member model data, since the data set is rather large. So, I've set up the extra routes and functions. But when I attempt to link to the edit2 resource, Laravel will not create the proper link. I don't know what I'm doing wrong. Code:
Route:
Route::get('members.edit2', array('as'=>'edit2', 'uses'=> 'MembersController@edit2'));
Route::resource('members','MembersController');
MembersController:
// Regular edit function -- works just fine:
public function edit($id)
{
$member = $this->member->find($id);
return View::make('members.edit', array(
'member'=>$member, ...
));
}
// Extra edit2 function -- should work if I could successfully route to it:
public function edit2($id)
{
$member = $this->member->find($id);
return View::make('members.edit2', array(
'member'=>$member, ...
));
}
show.blade.php:
// normal edit link (works fine, see source code below):
<a href="{{ URL::route('members.edit',$member->id) }}">edit</a>
// additional edit2 link (creates a bad link, see source code below):
<a href="{{ URL::route('edit2',$member->id) }}">edit</a>
source code:
// normal link that uses edit for member id=27:
<a href="https://zocios.com/members/27/edit">edit</a>
// link that attempts to use edit2 for same member:
<a href="https://zocios.com/members.edit2?27">edit</a>
I'm sure there is a way of doing this. It doesn't matter whether I use the named route 'edit2' rather than 'members.edit2', the exact same bad link is created. I've tried every combination I can think of. Laravel docs are not at all helpful for this. Thanks!
Upvotes: 0
Views: 181
Reputation: 1545
Your don't declare your edit2
route as you should do. Your first mistake is that the member's id
you want to edit is not passed as a parameter and the second one that by calling this {{route('edit2')}}
Laravel expects a url like /members.edit2
which is never going to appear. You should better use sth like /members/{id}/edit2
.
Try using this:
Route::get('members/{id}/edit2', array('as'=>'edit2', 'uses'=> 'MembersController@edit2'));
and call it like:
{{ route('edit2', [$id]) }}
Also be careful, whenever you call Url::route()
or simply route()
you should pass their parameters in an array like:
{{route('myRoute', ['par1', 'par2', 'par3', ...]}}
Upvotes: 1