简体   繁体   中英

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>

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 .

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

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM