M a m a D
M a m a D

Reputation: 2139

Laravel - route("resource.destroy") calls "resource.show"

This is the web.php

Route::group(['middleware' => 'auth'],
    function () {
        Route::get('letters/getRows', 'LetterController@getRows')->name('letters.getRows');
        Route::get('letters/{letter}/A4', 'LetterController@A4')->name('letters.A4');
        Route::get('letters/{letter}/A5', 'LetterController@A5')->name('letters.A5');
        Route::resource('letters', 'LetterController');
    }
);

I created a link as follow

"<a class='mx-2 h5' href='".route('letters.destroy', $entity->id)."'><i class='icon-remove-circle'></i></a>".

where the $entity->id is the id of the letter. The problem is, it links to show method not the destroy method. What can I do?

Using a form like this

{{ Form::open(array('route' => array('letters.destroy', $entity->id), 'method' => 'delete')) }}
    <button type="submit" >Delete Account</button>
{{ Form::close() }}

may solve the problem but I want to use a tag not a form.

update

In the php artisan route:list, the url of destroy and show are the same

enter image description here

thanks

Upvotes: 1

Views: 3000

Answers (2)

Hamoud
Hamoud

Reputation: 1929

When you use the Route::resource method it will create, among others, a route to DESTROY a resource like this: /letters/:id/ and another route to EDIT the resource: /letters/:id, and one more to SHOW /letters/:id

They all look the same. However, the difference is in the HTTP method/verb used to reach each route.

If you look to the output if php artisan route:list, you will find the list of HTTP methods used. Something like:

GET|HEAD  | letters/{letter} | letters.show 
PUT|PATCH | letters/{letter} | letters.update
DELETE    | letters/{letter} | letters.destroy

Therefore, to show a letter, you use a GET method, to edit a letter, use PUT method, and to destroy/delete, you use a DELETE method.

When you use an a tag, the browser will use the GET method, thus will reach the letters.show route. Html forms, can use POST or GET. Finally to use the DELETE http method, you need a form with hidden input named _method and the value="delete inside the form. Check the docs for more details.

There is also a note about this in LaravelCollective package documentations

Note: Since HTML forms only support POST and GET, PUT and DELETE methods will be spoofed by automatically adding a _method hidden field to your form.

Finally, if you must use an anchor tag <a>, you could use javascript to listen to the click event and submit a form with DELETE method.

Update to add an example: You can find an example of using an anchor tag to submit the form, in the default app layout in the framework here

And this is a modified version to submit a delete request:

<a class="dropdown-item" href="#"
   onclick="event.preventDefault();
                                     document.getElementById('destroy-form').submit();">
    {{ __('DELETE') }}
</a>

<form id="destroy-form" action="{{ route('letters.destroy', $entity) }}" method="POST" style="display: none;">
    @method('DELETE')
    @csrf
</form>

Upvotes: 5

lagbox
lagbox

Reputation: 50491

You cant. If you want to make a DELETE request you need to spoof it via a form (method POST, _method DELETE) or use Javascript.

Hyperlinks will cause new requests which will be GET requests. That is just how the web works.

Upvotes: 2

Related Questions