Ad

Sometimes Laravel Calls Another Route Instead Of Real One

- 1 answer

In laravel routes.php file, I have below code:

// User Resourceful Routing
Route::resource('user','UserController');

By default delete route goes to show method of UserController. To overcome this problem I have written this route in laravel routes.php:

// Delete Users Route
Route::get('user/{user}/{flag}', ['uses' => '[email protected]', 'as'=>'user.destroy'])->before('permission:user_delete');

Still It works fine for me but sometimes (2 out of 10 times) it goes to show method of UserController instead of destroy method.

// Destroy method
public function destroy($id,$flag='normal'){
}

Your suggestions are highly appreciated.

Ad

Answer

You should use 'delete' method instead of get.

You can use in laravel like this.

{!! Form::open(['action' => ['[email protected]', $user->id], 'method' => 'delete']) !!}
  {!! Form::submit('Delete User') !!}
{!! Form::close() !!}

And if you are using ajax you can use like this .

$.ajax({
        type: "DELETE",
        url: 'users/' + user_id,
        success: function(affectedRows) {
        }
    });

If you dont want to use laravel's default destroy method then you can use delete_user method with post action.

Ad
source: stackoverflow.com
Ad