php Laravel 5.1 从数据库中删除一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31587193/
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-25 22:32:45  来源:igfitidea点击:

Laravel 5.1 Delete a row from database

phplaravel-5laravel-routing

提问by vignesh pbs

I am trying to delete a category by clicking on a button

我正在尝试通过单击按钮来删除类别

Blade:

刀刃:

<td class="center"><a href="{{URL::to('/deletecat/'.$category->name) }}"><span class="glyphicon glyphicon-trash"></span></a></td>

Route:

路线:

Route::get('/deletecat/{name}','CategoryController@delete');

Controller:

控制器:

 public function delete($name)
    {

        category::find($name)->delete();

        return Redirect::route('managecategory');

    }

but I am getting an error while clicking on a button that

但是我在点击一个按钮时出现错误

Call to a member function delete() on a non-object

Any help appreciated.

任何帮助表示赞赏。

回答by Jerodev

The ::find($id)method expects $idto be a number, the primary key of the row you want to find.

::find($id)方法需要$id一个数字,即您要查找的行的主键。

If you want to delete a row by name, you should use the following code:

如果要按名称删除一行,则应使用以下代码:

category::where('name', $name)->delete();