Reputation: 1599
I don't think this post How do I override laravel resource route default method? solves my problem.
Normal resource route is that "index" shows all items. What I want to do is have "index" show all related items for a particular id.
So when I select a classroom from the list, I want the index action that I'm calling, to show, as it's index function, all the people for that specific classroom.
So I replaced the default resource route
//Route::resources(['attendees' => 'attendeesController']);
with
Route::resource('attendees', 'attendeesController')->names([
'index' => 'attendees.index',
'store' => 'attendees.store',
'create' => 'attendees.create',
'show' => 'attendees.evaluation',
'update' => 'attendees.update',
'destroy' => 'attendees.destroy',
'edit' => 'attendees.edit',
]);
So in my controller, I have this:
public function index(Request $request,$id)
{
dd($request);
...
}
And in my view of classrooms, on a particular classroom id I have this
<a href="{{route('attendees.index', ['classroom' => $data->id])}}">{{$data->Reference}}
So why am I getting this? I'm guessing something pretty basic, but I can't see what.
Type error: Too few arguments to function
App\Http\Controllers\AttendeesController::index(),
1 passed and exactly 2 expected
Upvotes: 2
Views: 4512
Reputation: 320
By default index action is expecting an$id
so you can set that to null
public function index(Request $request,$id = null)
Also, if you want to fetch related items for a particular $id
according to the docs URL would be attendees/123
which will be redirected to show
function. So you need to edit that route as well. Instead of that try passing a query param to index route and using the query param you can get the related data.
instead of
attendees/123
it will be attendees?id=123
Query param is set to display the related items, else display the indexes. if you still want to achieve it via index you need to change the routes as below
Route::resource('attendees', 'AttendeesController',['only' => ['index', 'create', 'store']]);
Route::get('/attendees/{id}', 'AttendeesController@index');
Upvotes: 3
Reputation: 77
Because you have passed in only 1 parameter. The method "index" in the controller is expecting 2 parameters. You might want to check your route.php file. https://laravel.com/docs/5.6/routing
Upvotes: 0