0

I've gotten stuck on a passing additional parameters to a named route. I have found how to do it on Laravel doc's as well as an answer on Stack Overflow answer.

My problem is I cannot get to my delete function in the controller, when I click the link the page refreshes and throws no errors but does not get to the controller.

What could be wrong with my route?

Route:

Route::delete('/assets/{asset}/{model}', 'AssetManagmentController@destroy')->name('asset.delete');

Href:

<td data-label="Destroy:"><a href="{{ route('asset.delete', ['asset' => $row->id, 'model' => $key] ) }}" data-method="DELETE" data-destoy='destroy' name="delete_item">Destroy</a></td>

<td data-label="Destroy:"><a href="{{ route('asset.delete', ['asset' => 'id', 'model' => 'model'] ) }}" data-method="DELETE" data-destoy='destroy' name="delete_item">Destroy</a></td>
halfer
  • 19,824
  • 17
  • 99
  • 186
Jelly Bean
  • 1,161
  • 1
  • 11
  • 22
  • 1
    You are using delete route while you are sending get request either change route to get or your request to delete request – Salman Zafar Jul 30 '19 at 06:52
  • (Once you get answer(s) below, there is no need to copy that into your question. Self-answers are welcome here, but please make them in an answer post). – halfer Jul 31 '19 at 21:33

4 Answers4

3

data-method="DELETE" will not magically make your link do a DELETE request. Anchor tags can only send GET requests.

You will either have to create a GET route that can be used with an anchor tag, or create a form that can be spoofed to send a DELETE request.

Jerodev
  • 32,252
  • 11
  • 87
  • 108
  • Any suggestions on the correct way of passing an additional parameter to the delete route? – Jelly Bean Jul 30 '19 at 07:05
  • 2
    You are already passing the parameters correctly to your delete route. It's just the request type that is not correct. – Jerodev Jul 30 '19 at 07:08
2

i have tested in my system and it's worked.

here my woking code

<td>
  <form method="post" id="delete-form-{{ $post->id }}" action="{{ route('post.destroy', $post->id) }}" style="display: none;"> @csrf @method('DELETE') </form>
  <a href="javascript: void(0);" onclick="if(confirm('Are you sure, You want to delete this?')) { event.preventDefault(); document.getElementById('delete-form-{{ $post->id }}').submit(); }">
    <span class="fa fa-trash"></span>
  </a>
</td>

i hope this is helps you

Mitesh Rathod
  • 879
  • 5
  • 18
1

On based on route you mention in question, need to create form with delete request.

like.

<td data-label="Destroy:">
{{ Form::open(['route' => ['asset.delete', $row->id, $key], 'method' => 'delete']) }}
<button type="submit">Destroy</button>
{{ Form::close() }}
</td>
Shivendra Singh
  • 2,986
  • 1
  • 11
  • 11
0

As per your information is given, u Juts need to change in HREF of anchor tag change route to route('asset/assets_id/Modelname').

Route::delete('/asset/{id}/{model}','AssetManagmentController@destory')->name('assets.delete');

<td data-label="Destroy:"><a href="{{ route('asset/assets_id/Modelname') }}" data-method="DELETE" data-destoy='destroy' name="delete_item">Destroy</a></td>