Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何计算数据库中的结果?_Php_Laravel - Fatal编程技术网

Php 如何计算数据库中的结果?

Php 如何计算数据库中的结果?,php,laravel,Php,Laravel,我想计算从数据库中获取的结果 $t = Activation::where('user_id', '=', $user->id)->first(); $w=(count($t)); dd($w); 我希望看到获取的结果数量您的代码是错误的。。。请阅读 使用第一个函数,您只获得查询返回的集合的第一个结果。要使代码正常工作,应该使用get函数。dd$w将返回正确的结果 无论如何,实现您的目标有一些具体的方法,只需从 激活::其中'user_id','=',$user->id->fir

我想计算从数据库中获取的结果

$t = Activation::where('user_id', '=', $user->id)->first();

$w=(count($t));
dd($w);

我希望看到获取的结果数量

您的代码是错误的。。。请阅读

使用第一个函数,您只获得查询返回的集合的第一个结果。要使代码正常工作,应该使用get函数。dd$w将返回正确的结果

无论如何,实现您的目标有一些具体的方法,只需从

激活::其中'user_id','=',$user->id->first; //输出:{user_id:1,电子邮件:'test@example.com', [...]} 到

激活::其中'user_id','=',$user->id->count; //产出:123
尝试使用拉维计数法

$t = Activation::where('user_id', '=', $user->id)->count();
dd($t);

$ty = Activation::where('user_id', '=', $user->id)->count();
dd($ty);
您使用的是第一个,如果找到它,它将只返回一个对象。使用get,然后使用count

$t = Activation::where('user_id',$user->id)->get();
$w = count($t);
如果您只想计数,请使用计数


->首先返回一个结果,您到底想在这里计算什么?如果您想计算查询结果的数量,只需使用->像这样的计数:Activation::where'user_id'、'='、$user->id->count,但您的示例不合理,因为user_id应该是唯一的,并且始终返回1或0$w=count$t@阿米瑞扎姆解决了这个问题
Activation::where('user_id',$user->id)->count();