1

I am creating Elequent models with the following structure:

Event->hasMany(Invite)
Invite->belongsTo(Event)

I am creating a controller with the following artisan command:

php artisan make:controller -mInvite -pEvent EventInvitesController --resource

Now if I create custom routes like:

Route::get('event/{event}/invite', 'EventInvitesController@index')->name('event.invites.index');
Route::post('event/{event}/invite', 'EventInvitesController@store')->name('event.invites.store');
Route::get('event/{event}/invite/{invite}', 'EventInvitesController@show')->name('event.invites.show');
Route::put('event/{event}/invite/{invite}', 'EventInvitesController@update')->name('event.invites.update');
Route::delete('event/{event}/invite/{invite}', 'EventInvitesController@destroy')->name('event.invites.destroy');
Route::get('event/{event}/invite/{invite}/edit', 'EventInvitesController@edit')->name('event.invites.edit');

Then everything works fine. But I was wondering if there is a way to do this like

Route::resource('event-invites', 'EventInvitesController');

When I've tried to do this the routes only have a single {event_invites} paramater when I need two.

Is there a way to enable routing for the parent model in Route::resource()? If not how would I go about extending Route to provide such a method?

kaan_a
  • 3,503
  • 1
  • 28
  • 52

1 Answers1

1

Use dot notation:

Route::resource('events.invites', 'EventInvitesController');

This would create a set of routes for posts that include the user identifier. For example:

Check out the documentation here:

https://laravel.com/docs/5.1/controllers#restful-nested-resources

Sapnesh Naik
  • 11,011
  • 7
  • 63
  • 98
  • @Kaan Happy coding :) If you're interested much about Laravel, do check my blog: kerneldev.com – Sapnesh Naik May 18 '19 at 13:34
  • Why is this not documented anymore in later Laravel versions (after version 5.1)? It still works, that is not my question. Is there some new philosophy perhaps? – Ametad May 21 '19 at 13:27
  • @Ametad I found that strange too. I can't guess why thought, my first thought was they forgot to include it?. I don't know. You can try opening an issue on GitHub if you really want to know why. – Sapnesh Naik May 21 '19 at 18:04
  • Here it is removed: https://github.com/laravel/docs/commit/d9fab42cffb0dce74322b7bb7b94c8828c156aae – Ametad May 23 '19 at 21:38