Jelly Bean
Jelly Bean

Reputation: 1219

Laravel pass multiple parameters to named route

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>

Upvotes: 0

Views: 1218

Answers (4)

Mitesh Rathod
Mitesh Rathod

Reputation: 1059

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

Upvotes: 2

Shivendra Singh
Shivendra Singh

Reputation: 3006

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>

Upvotes: 1

Zubair Lone
Zubair Lone

Reputation: 1

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>

Upvotes: 0

Jerodev
Jerodev

Reputation: 33196

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.

Upvotes: 3

Related Questions