Laravel DB::update 只返回 0 或 1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38551397/
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 DB::update only return 0 or 1
提问by Shahid Chaudhary
Laravel DB update
function returns 0 if the query failed or if the row was not affected because update values are same.
update
如果查询失败或由于更新值相同而行未受影响,Laravel DB函数将返回 0。
How can I check if the query failed because of an error or because the row was not affected? In the below code, if values are the same, it will return and won't run the next set of code in the controller.
如何检查查询是否因错误或行未受影响而失败?在下面的代码中,如果值相同,它将返回并且不会在控制器中运行下一组代码。
$query = DB::table('users')
->where('id', '=' , $requestdata['user_id'] )
->update([
'first_name' => $requestdata['user_fname'],
'last_name' => $requestdata['user_lname'],
]);
if ( !$query ) {
return ['error' => 'error update user'];
}
回答by Leon Vismer
An update query returns the number of rows that where affected by the update query.
更新查询返回受更新查询影响的行数。
$returnValue = DB::table('users')
->where('id', '=', $data['user_id'] )
->update([
'first_name' => $data['user_fname'],
'last_name' => $data['user_lname'],
]);
So the above will return the number of records that where updated. 0
means that no records have been updated.
所以上面将返回更新的记录数。0
表示没有记录被更新。
However, for you it might also not signify an error even if a 0
was returned as it just means that no records where updated. So if you try to update the name and lastname of a user with the details it actually already contains the query will return 0
.
但是,对于您来说,即使0
返回了 a 也可能不表示错误,因为它只是意味着没有更新的记录。因此,如果您尝试使用实际已包含的详细信息更新用户的姓名和姓氏,则查询将返回0
。
Useful side note, play with php artisan tinker
, ie.
有用的旁注,玩php artisan tinker
,即。
>>> $retval = DB::table('users')->where('id', 1)->update(['first_name' => 'Testing']);
=> 1
run the same query again and you will notice it returns 0
affected rows as the name is already Testing
再次运行相同的查询,您会注意到它返回0
受影响的行,因为名称已经Testing
>>> $retval = DB::table('users')->where('id', 1)->update(['first_name' => 'Testing']);
=> 0
Updated addition:
更新补充:
So for you it might be better to first check if the user exists with either a User::findOrFail($id)
or just check the return of User::find($id)
and then if the user exists you do your conditional update on the returned $user
. It is more explicit as update returns the same 0
for a non-existent user and a if the user details already where what you are trying to set it to.
因此,对您来说,最好先检查用户是否存在 aUser::findOrFail($id)
或仅检查返回,User::find($id)
然后如果用户存在,则对返回的$user
. 它更明确,因为更新0
为不存在的用户返回相同的信息,并且如果用户详细说明您尝试将其设置到的位置。
回答by Kannan K
You can use try catch:
您可以使用 try catch:
try {
$query = DB::table('users')
->where('id', '=' , $requestdata['user_id'] )
->update([
'first_name' => $requestdata['user_fname'],
'last_name' => $requestdata['user_lname'],
]);
} catch(\Illuminate\Database\QueryException $ex){
return ['error' => 'error update user'];
}
回答by Ravshanbek Ahmedov
Easy way do update if same values:
如果相同的值,简单的方法进行更新:
App\User::find($requestdata['user_id'])->update([
'first_name' => $requestdata['user_fname'],
'last_name' => $requestdata['user_lname'],
'updated_at' => Carbon::now()
]);
回答by Christian Carrillo
working for me using the tap helperat laravel 6.x:
在 laravel 6.x 上使用tap 助手对我来说有效:
return tap(User::findOrFail($id))->update($request->all());