繁体   English   中英

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

[英]How to count results from database?

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

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

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

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

你的代码错了……请阅读Laravel官方文档

使用first() function 您将获得从查询返回的集合的第一个结果。 要使您的代码正常工作,您应该使用get() function。 并且dd($w)将返回正确的结果。

无论如何,有特定的聚合函数可以实现您的目标,只需将您的代码从

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

// output: {"user_id": 1, "email": 'test@example.com', [...]}

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

// output: 123

您可以使用 laravel 中的计数方法来计算结果。

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

尝试使用 laravel 计数方法。

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

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

您正在使用first()如果找到,它将只返回一个 object。 使用get()然后使用计数。

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

如果您只想计数,请使用count()

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

更改您的代码

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM