A.J
A.J

Reputation: 1180

Laravel Route Resource both GET and POST

I have this Route

Route::group([ 'middleware' => ['auth','lang']], function() {

    // SETTINGS
    Route::namespace( 'Settings' )->prefix( 'settings' )->group( function () {

        // INDEX
        Route::get( '/', 'SettingsController@index' );

        // ACCOUNTS
        Route::resource( 'accounts', 'AccountController', ['only' => ['index','store','edit','update']] );

        // TAGS
        Route::resource( 'tags', 'TagController', ['only' => ['index','destroy']] );

        // PROFILE
        Route::get('profile', 'ProfileController@index');
        Route::post('profile', 'ProfileController@update');

    }); 

Any way I can join the two PROFILE ones into one that is resource? Whenever I try using Route::resource( 'profile', 'ProfileController', ['only' => ['index','update']] ), it gives me an error that the method is not allowed - 405 (Method Not Allowed). I think it just doesn't find the update one? I am really not sure what might be the issue.

Upvotes: 0

Views: 3018

Answers (1)

pseudoanime
pseudoanime

Reputation: 1593

This is happening because in the case of resourceful controllers, a post would be defaulted to a store method, not update.

So you are posting to the store method, which is not defined, giving you the 403 method not allowed.

To solve this, either change your request to a PUT or change your code to Route::resource( 'profile', 'ProfileController', ['only' => ['index','store']] ) Keep in mind, if you do this, you have to move the contents of your update function to store.

For more information, checkout https://laravel.com/docs/5.5/controllers#resource-controllers

Upvotes: 2

Related Questions