【发布时间】:2021-04-19 12:59:19
【问题描述】:
我正在尝试在表视图中显示数据库中的唯一值以及它们的数量。但我有问题计算它们。
id | name |
------------
1 | john |
2 | john |
3 | smith |
我的目标是显示在表格中
name count
---------------
john 2
smith 1
my controller
$usersList = DB::table('users')->distinct('name')->get('name');
//dd($usersList);
return view('dashboard', compact('data'))->with(['usersList '=> $usersList]);
dd($userList) 显示 1 john 和 1 smith
dashboard.blade
@foreach ($usersList as $row)
<tr>
<th scope="row">{{ $row ->name }}</th>
<td>{{ $row->count('name') }}</td>
</tr>
@endforeach
error : Call to undefined method stdClass::count()
【问题讨论】: