Reputation: 405
I'm new to laravel and I am making a project. and in the project I have a table with records. I also have a delete button in every row of the table. When I click this button I want it to delete the record, however, it is not working. When I click the delete button it sends me to a different page and gives me a 404 error. I really appreciate if someone could help me with my problem.
My view :
<body>
<div class="spelers">
@if (count ($spelers) > 0)
<table id="table" class="table table-hover">
<thead>
<th scope="col">Selectie</th>
<th scope="col"></th>
</thead>
@foreach ($spelers as $speler)
<tr><td><a>{{{ $speler->speler_naam }}}</a></td><td><img src="../../img/edit.png" alt=""></a></td><td><a href="delete/{{ $speler->id }}"><img src="../../img/delete2.png" alt=""></a></td></tr>
@endforeach
</table>
@else
Er zijn helaas geen clubs beschikbaar
@endif
</div>
</div>
My controller :
public function Destroy($id)
{
SpelerSelectie::where('id', $id)->delete();
return redirect ('/');
}
My routes :
Route::get('/delete/{id}', 'VoetbalController@Destroy');
Upvotes: 0
Views: 57
Reputation: 9303
Named routes allow the convenient generation of URLs or redirects for specific routes.
Route :
Route::get('delete/{id}', 'VoetbalController@destory')->name('voetbal.destroy');
Call :
<a href="{{ route('voetbal.destroy', $speler->id) }}">Delete</a>
Upvotes: 1
Reputation: 407
Since you do delete
without a /
the link will append the current page you are on. So if you are on /test
, then the url will be /test/delete
.
To make sure you always end up on the correct url path, you can simply generate the url in the blade file based on the action. It will look like this:
Change
<a href="delete/{{ $speler->id }}">
To
<a href="{{ action('VoetbalController@destroy', $speler->id) }}">
You can now also change the route and the link will still be correctly rendered.
Let me know how that works out.
Upvotes: 2
Reputation: 1188
It looks like your url is not being generated properly.
Use this helper method for generation urls.
url()
<a href="{{ url('delete/') . $speler->id }}">
Upvotes: 0
Reputation: 152
change
<a href="delete/{{ $speler->id }}">
To
<a href="/delete/{{ $speler->id }}">
Hope this will help you.
Upvotes: 0