Laravel 5:返回受影响的行数 MySQL

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

Laravel 5: return number of affected rows MySQL

phpmysqllaravellaravel-5

提问by mwafi

how to return something similar to PHP function mysql_affected_rows()when use Laravel 5 DB class,

mysql_affected_rows()使用 Laravel 5 DB 类时如何返回类似于 PHP 函数的内容,

ex: DB::delete("DELETE FROM chat WHERE user_id = {$mid}");

前任: DB::delete("DELETE FROM chat WHERE user_id = {$mid}");

how to return number of affected rows?

如何返回受影响的行数?

thanks,

谢谢,

回答by Joel Hinz

For update()and delete()calls, the return value is the number of affected rows.

对于update()anddelete()调用,返回值是受影响的行数。

$affected = DB::delete("DELETE FROM chat WHERE id = {$mid}");

回答by Matt Komarnicki

Did you try?

你试过了吗?

Official manual: The delete method should be used to delete records from the database. Like update, the number of rows deleted will be returned:

官方手册:从数据库中删除记录应该使用delete方法。和update一样,会返回删除的行数:

$deleted = DB::delete('delete from users');

$deleted = DB::delete('delete from users');

回答by Majbah Habib

You may get affected row by using Query builder

您可能会使用查询生成器获得受影响的行

For Laravel 5

对于 Laravel 5

$ids=[10,20,30,40];

$updatedRow=DB::update('update test_table set status = 1 where id != ?', $ids);

echo $updatedRow;
exit;