toddddos
toddddos

Reputation: 361

Laravel calling @show instead of @destroy

I have a problem with my routes in Laravel (v. 4.2)..

View:

{{ Form::open(['method' => 'DELETE', 'route' => ['admin.users.destroy', $user->user_id]]) }}
      <td><button type="submit" style="...">Delete</button></td>
{{ Form::close() }}

Route:

Route::resource('admin/users', 'App\Controllers\Admin\UserIndexController');

Controller:

public function show() {
    echo "show";
}

public function destroy() {
    echo 'destroy';
}

When the button is clicked though it always prints out "show". Why is this?

Upvotes: 0

Views: 1178

Answers (2)

user1669496
user1669496

Reputation: 33068

HTML forms cannot make PUT, PATCH, or DELETE requests so you need to spoof it with Laravel.

Add this to your form...

<input type="hidden" name="_method" value="delete" />

I believe this generally isn't an issue with newer versions of Laravel since the Form builder has been removed from the core and is now being managed by LaravelCollective which will handle adding this input automatically.

https://laravel.com/docs/4.2/html#opening-a-form

Upvotes: 1

Shenbaga Ganesh
Shenbaga Ganesh

Reputation: 16

Edited Answer:

Routes.php

Route::resource('admin/users', 'UserIndexController');

{{ Form::open(['method' => 'DELETE', 'route' => ['admin.users.destroy', $user->user_id]]) }}
      <td><button type="submit" style="...">Delete</button></td>
{{ Form::close() }}

public function show() {
        echo "show";
    }

    public function destroy() {
        echo 'destroy';
    }

I am getting "destroy" as output.

Upvotes: 0

Related Questions