簡體   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