0

I have resource route like this

Route::resource('chapter', 'ChapterController');

I want to pass parameter id to it like this

Route::resource('chapter/{id}', 'ChapterController');

and use it in my controller like this

public function index($id)
    {
        $subject=Subject::find($id);
        $chapter=Chapter::where('subject_id',$id)->get();
        return view('chapter.index',[
            'subject'=>$subject,
            'chapter'=>$chapter
        ]);
    }

Please help me

  • what framework is it about? – Capitaine Feb 09 '20 at 09:06
  • @Capitaine Laravel. – Rwd Feb 09 '20 at 09:09
  • Will you be passing the subject id to all of the methods in your `ChapterController` or just the `index()` method? – Rwd Feb 09 '20 at 09:11
  • Laravel resource routes are shorthand for a specific set of routes seen [here](https://laravel.com/docs/6.x/controllers#resource-controllers) if you want to override them you can specify a [partial resource route](https://laravel.com/docs/6.x/controllers#restful-partial-resource-routes) and then define your resources manually. – apokryfos Feb 09 '20 at 10:15

4 Answers4

1

Route::resource which is called RESTful resource controller sets up default routes and handles the given below actions.

Verb          Path                                     Action                       Route Name

GET           /chapter                                 index                        chapter.index

GET           /chapter/create                          create                       chapter.create

POST          /chapter                                 store                        chapter.store

GET           /chapter/{id}                            show                         chapter.show

GET           /chapter/{id}/edit                       edit                         chapter.edit

PUT|PATCH     /chapter/{id}                            update                       chapter.update

DELETE        /chapter/{id}                            destroy                      chapter.destroy

If you need to add additional routes to a resource controller beyond the default set of resource routes, you should define those routes before your call to Route::resource; otherwise, the routes defined by the resource method may unintentionally take precedence over your supplemental routes:

Route::post('chapter/{id}', 'ChapterController@method')->name('chapter.action');
//...
Route::resource('chapter', 'ChapterController');
Foued MOUSSI
  • 4,643
  • 3
  • 19
  • 39
0

Your index function has to be with Request parameter also: index(Request $request, $id)

Kancho Iliev
  • 701
  • 5
  • 13
0

make sure all methods of resource controller pass id to it

Mustafa Goda
  • 109
  • 6
0

If you want to use id for your index method and already declared a resource route then you need to use custom route, because laravel resource index route doesn't support id or any other parameter

//your Resource Route
Route::resource('chapter', 'ChapterController');

//Custom `route` for your `index` method it will replace your `resource index route`
Route::get('chapter/{id}', ['as'=>'chapter.index','uses'=>'ChapterController@index']);
Md Abdul Awal
  • 512
  • 2
  • 9