phplaravelcontrollerresourcesrequest

Laravel post directly to resource index


I have one, hopefully small, issue. I need to be able to post directly to my index. Right now I am using a resource controller:

Route::resource('appointments', 'AppointmentsController');

And I want to be able to update my index view from my resource controller using dropdowns to post values to my index. So I can use those values like so:

public function index(Request $request)

Up untill this point I was using a different route to post to, and then redirect to my appointments.index route, and so on. But that is silly. I want to still be able to use my resource controller, otherwise I need to create lots of routes (since I am using a bunch of resource controllers and I need to be able to post directly to the index on all of them).

What is the most efficient way to resolve this? I did try to open my form using an url and then add a trailing slash at the end, but that did not do the trick.


Solution

  • It's quite simple, just create your Resource controller without the store route, like this:

    Route::resource('appointments', 'AppointmentsController', ['except' => ['store']]);
    

    Then add your route before the resource declaration, something like this:

    Route::post('appointments', 'AppointmentsController@index');
    Route::resource('appointments', 'AppointmentsController', ['except' => ['store']]);