[英]How to count results from database?
I want to count the result fetched from database我想计算从数据库中获取的结果
$t = Activation::where('user_id', '=', $user->id)->first();
$w=(count($t));
dd($w);
I expect to see the number of results fetched我希望看到获取的结果数量
Your code is wrong... Please read the Laravel official documentation你的代码错了……请阅读Laravel官方文档
With first()
function you're getting just the first result of the set returned from your query.使用
first()
function 您将获得从查询返回的集合的第一个结果。 To make your code work you should use the get()
function.要使您的代码正常工作,您应该使用
get()
function。 and the dd($w)
will return the correct result.并且
dd($w)
将返回正确的结果。
Anyway there are specific aggregate functions to achieve your goal, just changing your code from无论如何,有特定的聚合函数可以实现您的目标,只需将您的代码从
Activation::where('user_id', '=', $user->id)->first();
// output: {"user_id": 1, "email": 'test@example.com', [...]}
to至
Activation::where('user_id', '=', $user->id)->count();
// output: 123
You can use count method in laravel to count result.您可以使用 laravel 中的计数方法来计算结果。
$t = Activation::where('user_id', '=', $user->id)->count();
Try to use a laravel count method.尝试使用 laravel 计数方法。
$t = Activation::where('user_id', '=', $user->id)->count();
dd($t);
$ty = Activation::where('user_id', '=', $user->id)->count();
dd($ty);
You are using first()
that will return only one object if found.您正在使用
first()
如果找到,它将只返回一个 object。 use get()
and then use count.使用
get()
然后使用计数。
$t = Activation::where('user_id',$user->id)->get();
$w = count($t);
If you just want to count then use count()
如果您只想计数,请使用
count()
Activation::where('user_id',$user->id)->count();
Change Your Code From更改您的代码
$t = Activation::where('user_id', '=', $user->id)->first();
To至
$t = Activation::where('user_id', '=', $user->id)->count();
dd($t);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.