How to pass value from a button to a controller method in laravel 5

5.7k Views Asked by At

I working on a simple laravel 5.2 application. I want to pass a value $id from the view when a button is clicked to the controller method destroy(). This is what i have tried: This is my route

Route::get('delete/{id}', array('as' => 'delete', 'uses' => 'ContactsController@destroy'));

... and this is my button in a view:

<a href="{{ action('ContactsController@destroy/{$contacts->id}/') }}" class="btn btn-danger">Delete</a>

But this code didn't work. Thanks for any help.

3

There are 3 best solutions below

0
On

According to the documentation you should be able to do this:

 {{ action('ContactsController@destroy', ['id' => $contacts->id]) }}
0
On

Your route is expecting an id so you don't need to pass an identifier. The below code should work for you.

Make sure you're using the correct brackets as by default in Laravel 5.2 you'll need to use {!! !!} instead of {{ }}.

<a href="{{ route('delete', $contacts->id) }}" class="btn btn-danger">Delete</a>
0
On

Since it is a GET request, you can simply have you own href instead of using blade. I just find this more readable:

<a href="/delete/{{$contacts->id)}}" class="btn btn-danger">Delete</a>