Laravel 对具有相同地址的用户进行计数,并将其返回到刀片视图中

Laravel 对具有相同地址的用户进行计数,并将其返回到刀片视图中,laravel,backpack-for-laravel,laravel-backpack,Laravel,Backpack For Laravel,Laravel Backpack,我有一个“病人”模型,其中包含每个病人的姓名、出生日期、地址等。我想将具有相同地址的患者分组,以便检测包含更多感染者的地址。因此,我创建了一个只显示列表操作的新模型,并在其中获得了以下查询: $this->crud->addClause('where', 'stato', '=','POSITIVE'); $this->crud->groupBy('address'); $this->crud->query->selectRaw('GROUP_CONCA

我有一个“病人”模型,其中包含每个病人的姓名、出生日期、地址等。我想将具有相同地址的患者分组,以便检测包含更多感染者的地址。因此,我创建了一个只显示列表操作的新模型,并在其中获得了以下查询:

$this->crud->addClause('where', 'stato', '=','POSITIVE');
$this->crud->groupBy('address');
$this->crud->query->selectRaw('GROUP_CONCAT(name SEPARATOR ", ") as names, id as ids, COUNT(*) as count');
$this->crud->orderBy('count','desc');
然后,我将我的列设置为:

protected function setupListOperation()
{
    $this->crud->addColumns([
    [
        'name'   => 'address',
        'label'  => 'Address',
        'orderable' => false,
    ],
    [
        'name'   => 'count',
        'label'  => 'Infected',
    ],
    [
        'name'   => 'names',
        'label'  => 'names',
        //'type'   => 'markdown',
        'orderable' => false,
    ]
]);
    $this->crud->enableDetailsRow();
    $this->crud->setDetailsRowView('vendor.backpack.crud.details_row.outbreaks');
}
这将返回一个地址列表及其计数,以及居住在该地址的一长串名称。 例如:

|地址|感染者|姓名| |-------------------|----------|----------------------------| |地址街道,4 | 35 |名称1、名称2、名称3等| 由于setupListOperation()似乎不方便列出长串的姓名,我想我应该创建一个自定义的详细信息行来列出居住在每个地址的所有患者

<div class="m-t-10 m-b-10 p-l-10 p-r-10 p-t-10 p-b-10">
<div class="row">
    <div class="col-md-12">
        <ul>
        {{-- @foreach($entries as $entry) --}}
        <li>
        <a href="positivi/{{ $entry->id }}/show">
        {{ $entry->name }}</a>
        <small>({{ $entry->age }} years old)</small>
        </li>
        {{--@endforeach--}}
    </ul>
    </div>
</div>

    {{--@foreach($entries as$entry)--}
  • ({{$entry->age}}}岁)
  • {{--@endforeach--}

但它只给了我一个地址的名字!我尝试传递查询变量“names”,并尝试在视图中执行foreach循环,但没有一个有效。列出居住在该地址的所有患者并通过href链接其id的最佳方式是什么?

我知道您想知道每个地址(位置)中有多少人受到感染,因此您可以通过以下方式开始收集受感染者的地址:

$addresses = Patient::where('stato', 'POSITIVE')->pluck('address');
然后,您可以在数组中循环以获取计数和感染者列表:

foreach($addresses as $address)
{
   // count the infected people in a location
   $count = Patient::where('address', $address)->where('stato','POSITIVE')->count();
   // get a list of the infected patients on a location
   $infected = Patient::where('address', $address)->where('stato', 'POSITIVE');

}

谢谢你的回答。当我使用Backpack for Laravel时,ListOperation函数已经在查询中循环,特别是我希望将该数据放入showDetailsRow视图中。是否可以使用背包,或者我是否应该覆盖循环并制作自己的表格?
foreach($addresses as $address)
{
   // count the infected people in a location
   $count = Patient::where('address', $address)->where('stato','POSITIVE')->count();
   // get a list of the infected patients on a location
   $infected = Patient::where('address', $address)->where('stato', 'POSITIVE');

}