我對Laravel框架很陌生,我只是在構建一個簡單的博客。我可以創建一個博客,顯示博客並顯示所有博客的概述。現在我想刪除一個博客。因此,我在視圖中創建了一個刪除按鈕,其中的路由鏈接也會傳遞文章的ID。然後,在我的路由文件中,我指定了一個刪除請求和一個控制器方法。在該方法中,我找到了id並嘗試刪除具有在route/view中指定的id的行。從Laravel 5.4的數據庫中刪除數據
這不起作用。它不是激活銷燬/刪除方法,而是顯示文章而不是刪除它,並激活show方法而不是delete方法。有人可以幫助我,我錯了什麼?
View.blade.php
<a href="{{route('nieuws.destroy', ['id' => $blog->id])}}" onclick="return confirm('Weet je dit zeker?')">
<i class="fa fa-trash"></i>
</a>
路線
Route::group(['middleware' => 'auth'], function() {
Route::get('/aanvragen', '[email protected]')->name('aanvragen.index');
Route::get('/logout' , 'Auth\[email protected]')->name('logout');
Route::get('/nieuws/toevoegen', '[email protected]')->name('blogs.add');
Route::post('/nieuws/store', '[email protected]')->name('nieuws.store');
Route::delete('/nieuws/{id}', '[email protected]')->name('nieuws.destroy');
});
Route::get('/nieuws', '[email protected]')->name('blogs.index');
Route::get('/nieuws/{blog}', '[email protected]')->name('blogs.show');
控制器方法
刪除/銷燬
public function destroy($id) {
$blog = Blog::find($id);
$blog->delete();
return redirect('/nieuws');
}
顯示
public function show(Blog $blog) {
dd('show');
return view('blogs.show', compact('blog'));
}
@Gijsberts你可能要重新檢查KUNAL的答案。允許GET請求以任何方式更改數據是不好的做法和安全風險。 – Robert