php Laravel Eloquent ORM delete() 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45458074/
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
laravel Eloquent ORM delete() method
提问by Evol Rof
Hi I am studying laravel. I use Eloquent ORM delete method but I get a different result.Not true or false but null. I set an resource route and there is a destroy method in UsersController.
嗨,我正在学习 Laravel。我使用 Eloquent ORM 删除方法,但我得到了不同的结果。不是真或假而是空。我设置了一个资源路由,在UsersController 中有一个destroy 方法。
public function destroy($id){
$res=User::find($id)->delete();
if ($res){
$data=[
'status'=>'1',
'msg'=>'success'
];
}else{
$data=[
'status'=>'0',
'msg'=>'fail'
];
return response()->json($data);
But I always get a response {"status":"0","msg":"failed"},the record in the database is deleted.
但是我总是得到响应{"status":"0","msg":"failed"},数据库中的记录被删除。
Then I use dd($res) .It shows null in the page.
然后我使用 dd($res) 。它在页面中显示为空。
But from the course I learn it returns a boolean value true or false.
但是从我学习的课程中,它返回一个布尔值 true 或 false。
Is there any error in my code?
我的代码有错误吗?
Can you tell me some other method that I can get a boolean result when I delete data from database?
你能告诉我一些其他的方法,当我从数据库中删除数据时我可以获得布尔结果吗?
回答by AddWeb Solution Pvt Ltd
I think you can change your query and try it like :
我认为您可以更改您的查询并尝试如下:
$res=User::where('id',$id)->delete();
回答by Sagar Gautam
At first,
首先,
You should know that destroy()
is correct method for removing an entity directly via object or model and delete()
can only be called in query builder.
您应该知道这destroy()
是通过对象或模型直接删除实体的正确方法,并且delete()
只能在查询构建器中调用。
In your case, You have not checked if record exists in database or not. Record can only be deleted if exists.
在您的情况下,您尚未检查数据库中是否存在记录。记录只有在存在时才能删除。
So, You can do it like follows.
所以,你可以像下面这样做。
$user = User::find($id);
if($user){
$destroy = User::destroy(2);
}
The value or $destroy
above will be 0 or 1 on fail or success respectively. So, you can alter the $data
array like:
$destroy
失败或成功时,该值或以上将分别为 0 或 1。因此,您可以更改$data
数组,例如:
if ($destroy){
$data=[
'status'=>'1',
'msg'=>'success'
];
}else{
$data=[
'status'=>'0',
'msg'=>'fail'
];
}
Hope, you understand.
希望你能理解。
回答by Abdelsalam Shahlol
LaravelEloquent provides destroy()
function in which returns boolean
value. So if a record existson the database and deletedyou'll get true
otherwise false
.
Laravel Eloquent提供destroy()
了返回boolean
值的函数。因此,如果数据库中存在记录并被删除,您将得到true
其他信息false
。
Here's an example using Laravel Tinker shell.
这是一个使用Laravel Tinker shell的示例。
In this case, your code should look like this:
在这种情况下,您的代码应如下所示:
public function destroy($id)
{
$res = User::destroy($id);
if ($res) {
return response()->json([
'status' => '1',
'msg' => 'success'
]);
} else {
return response()->json([
'status' => '0',
'msg' => 'fail'
]);
}
}
More info about Laravel Eloquent Deleting Models
有关Laravel Eloquent 删除模型的更多信息
回答by Talha F.
You can use :
您可以使用 :
$data = Model::findOrFail($id);
$data->delete();