php 使用 Laravel 5.4 从数据库中删除数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44757455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:01:50  来源:igfitidea点击:

Delete data from the database with Laravel 5.4

phplaravellaravel-5laravel-5.4

提问by Giesburts

I am sort of new to the Laravel framework and I am building just a simple blog. I can create a blog, show a blog and show a overview of all blogs. Now I would like to delete a blog. So, I have created a delete button in my view with a route link which will pass also the id of the article. Then, in my routes file I specify a delete request and a controller method. In the method I find the id and try to delete the row with the id I specified in the route/view.

我是 Laravel 框架的新手,我正在构建一个简单的博客。我可以创建博客、显示博客并显示所有博客的概览。现在我想删除一个博客。因此,我在视图中创建了一个删除按钮,其中包含一个路由链接,该链接还将传递文章的 id。然后,在我的路由文件中,我指定了一个删除请求和一个控制器方法。在该方法中,我找到了 id 并尝试删除具有我在路由/视图中指定的 id 的行。

This doesn't work. Instead of activate the destroy/delete method it shows the article instead of deleting it and activates the show method instead of the delete method. Can somebody help me out, What do I wrong?

这不起作用。它没有激活 destroy/delete 方法,而是显示文章而不是删除它,并激活 show 方法而不是 delete 方法。有人可以帮助我,我有什么错?

View.blade.php

查看.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

路线

Route::group(['middleware' => 'auth'], function () {

    Route::get('/aanvragen', 'aanvragenController@index')->name('aanvragen.index');

    Route::get('/logout' , 'Auth\LoginController@logout')->name('logout');

    Route::get('/nieuws/toevoegen', 'blogController@create')->name('blogs.add');

    Route::post('/nieuws/store', 'blogController@store')->name('nieuws.store');

    Route::delete('/nieuws/{id}', 'blogController@destroy')->name('nieuws.destroy');

});

Route::get('/nieuws', 'blogController@index')->name('blogs.index');

Route::get('/nieuws/{blog}', 'blogController@show')->name('blogs.show');

Controller methods

控制器方法

Delete/Destroy

删除/销毁

public function destroy($id) {

    $blog = Blog::find($id);

    $blog->delete();

    return redirect('/nieuws');

}

Show

展示

public function show(Blog $blog) {

    dd('show');


    return view('blogs.show', compact('blog'));

}

采纳答案by Robert

A delete()route requires you to POST your data.

一个delete()路线需要你来发表您的数据。

HTML forms only supports GET and POST, other methods like DELETE, PUT, etc are not supported, that's why Laravel uses the _methodto spoof methods which are not supported by HTML forms.

HTML 表单仅支持 GET 和 POST,不支持 DELETE、PUT 等其他方法,这就是 Laravel 使用_methodHTML 表单不支持的to spoof 方法的原因。

You do notwant use GET in these cases, since someone can send a user the url (http://yoursite.com/blog/delete/1) in an IM or via email. The user clicks and the blog is gone.

在这些情况下,您希望使用 GET,因为有人可以通过 IM 或电子邮件向用户发送 url ( http://yoursite.com/blog/delete/1)。用户点击,博客消失了。

Define your route as it would be when using resource controllers, so:

像使用资源控制器一样定义您的路由,因此:

Route::delete('/nieuws/{id}', 'blogController@destroy')->name('nieuws.destroy');

And either use a form with the delete method:

或者使用带有 delete 方法的表单:

// apply some inline form styles
<form method="POST" action="{{ route('nieuws.destroy', [$blog->id]) }}">
    {{ csrf_field() }}
    {{ method_field('DELETE') }}
    <button type="submit">Delete</button>
</form>

Or do some javascript magic as the link SR_ posted in his comment on your OP.

或者做一些javascript魔术作为链接SR_发布在他对你的OP的评论中。



One more thing, add some sort of validation in your destroy action. Right now when you provide a non-existing id or something else, you will get a 500 error, instead you want to have a 404.

还有一件事,在您的销毁操作中添加某种验证。现在,当您提供不存在的 id 或其他内容时,您将收到 500 错误,而您想要的是 404。

public function destroy($id)
{
    $blog = Blog::findOrFail($id);

    $blog->delete();

    return redirect('/nieuws');
}

回答by Ru Chern Chong

To use DELETEHTTP Verb, your form should consists of the POSTmethod and settings the method_field('DELETE')

要使用DELETEHTTP 动词,您的表单应包含POST方法和设置method_field('DELETE')

Example:

例子:

<form method="POST" action="{{ route('xxx.destroy', $xxx->id) }}">
    {{ csrf_field }}
    {{ method_field('DELETE') }}
</form>

回答by AddWeb Solution Pvt Ltd

I think you need to update your destroy function like:

我认为您需要更新您的销毁功能,例如:

public function destroy($id) {

    $blog = DB::table('blog')->where('id',$id)->delete();

    return redirect('/nieuws');

}

And update your view code like:

并更新您的视图代码,例如:

<a href="{{route('nieuws.destroy', [$blog->id])}}" onclick="return confirm('Weet je dit zeker?')">
  <i class="fa fa-trash"></i>
</a>

Hope this work for you!

希望这对你有用!

回答by Tan Yuanhong

I'm also new to Laravel but I made it work through this way: (I use 'Article' as the model's name and the resource"method" in the route stands for a bunch of useful routes including the route you wrote)

我也是 Laravel 的新手,但我通过这种方式使它工作:(我使用“文章”作为模型的名称,resource路线中的“方法”代表一堆有用的路线,包括您编写的路线)

Controller:

控制器:

public function destroy($id){
        Article::find($id)->delete();
        //$article = Article::find($id);
        return redirect()->back()->withErrors('Successfully deleted!');
    }

Route:

路线:

Route::resource('article','ArticleController');

However, I think the problem lies in the default definition of database's name of your model. Laravel will assume that you have a database named blogs since you have a model named "blog". Are you having the database's name right?

但是,我认为问题在于模型的数据库名称的默认定义。Laravel 会假设您有一个名为 blogs 的数据库,因为您有一个名为“blog”的模型。你有数据库的名字吗?