Laravel 在 blade 中打印出数据库中唯一元素的计数

Laravel printing out in the blade the count of a unique element in the db

我有一个 table 有几个不同的域。我想要做的是打印出每个唯一域以及该域在我的数据库中出现的次数。例如,如果我在我的数据库中有 4 次测试域,它将打印出

test 4

我有它所以我打印出唯一值,但不知道如何打印出该值在我的 table

中出现的次数

这是我的控制器代码

public function updateExpirationDate()
{
    $users = User::select('domain')->distinct()->get();
   // dd($users);
    return view('admin.updatebulkexpirationdate', compact('users'));

}

这是我的 blade 代码

@foreach ($users as $user)
        <tr id='{{ $user->domain }}'>
            <td>{{ $user->domain }}</td>
            <td>{{ $user->domain->count()}}</td> //How Do I get the count of each domain
        </tr>
    @endforeach

加载数据:

$users = User::select('domain')->get();

使用收集方法where() and count():

{{ $user->where('domain', $user->domain)->count() }}

请注意,此处您正在处理加载的数据,而不是执行其他查询。