如何追加 Laravel 集合查询结果

How to Append Laravel Collection Query Results

我有以下查询:

$controls_checklists = collect();

foreach($active_evaluation_session->evaluation_items as $evaluation_item) {
    $controls_checklists->push(ControlsChecklist::where('macro_process_id', $evaluation_item->macro_process_id)
        ->where('process_id', 'like', $evaluation_item->process_id ? $evaluation_item->process_id : '%')
        ->where('status', 1)->get());
}

$controls_checklists->all();


return view('auto-evaluations.index', compact('controls_checklists'));

在我看来,我有:

@foreach ($controls_checklists as $controls_checklist)
<tr data-entry-id="{{ $controls_checklist->id }}">
    <td></td>
    <td>{{ $controls_checklist->control->control_reference }}</td>
    <td>{{ $controls_checklist->macro_process->name }}</td>
    <td>{{ $controls_checklist->process->name }}</td>
    <td>{{ isset($controls_checklist->sub_process->name)? $controls_checklist->sub_process->name: '' }}</td>
    <td>{{ isset($controls_checklist->activity->name)? $controls_checklist->activity->name: '' }}</td>
    <td>{{ $controls_checklist->checkpoint }}</td>
    <td>
        @if($controls_checklist->status == 1) {{ 'Active' }} @else {{ 'In Active' }} @endif
    </td>

    <td>
        <a href="{{ route('controls-checklists.edit',[$controls_checklist->id]) }}" class="btn btn-xs btn-info">@lang('global.app_evaluate')</a>
    </td>
</tr>
@endforeach

但是,当我打开页面时收到错误消息

Property [id] does not exist on this collection instance. (View: /opt/lampp/htdocs/controls-assessment/resources/views/auto-evaluations/index.blade.php) 

对于这一行

<tr data-entry-id="{{ $controls_checklist->id }}">

我认为由于我尝试合并上面的集合查询的方式,这不起作用。我该如何解决这个问题?

首先 dd($controls_checklist); 并验证 $controls_checklist->id 处的 ID 确实存在。如果不改变foreach循环语句。

进入foreach循环,尝试使用带有join或innerjoin的模型来管理查询结果

谢谢

您将不会成功循环,因为您的变量正在返回一个集合的集合。

如果你愿意,可以试试这个。

foreach($active_evaluation_session->evaluation_items as $evaluation_item) {
   $controlsChecklist = ControlsChecklist::where('macro_process_id', $evaluation_item->macro_process_id)
            ->where('process_id', 'like', $evaluation_item->process_id ? $evaluation_item->process_id : '%')
            ->where('status', 1)->get();
   foreach ($controlsChecklist as $value) {
     $controls_checklists->push($value);
  }
}

您不必在新的集合变量中收集数据。

试试这个:

$query = ControlsChecklist::query();

foreach($active_evaluation_session->evaluation_items as $evaluation_item) {
    $query->where('macro_process_id', $evaluation_item->macro_process_id)
        ->where('process_id', 'like', $evaluation_item->process_id ? $evaluation_item->process_id : '%')
        ->where('status', 1);
}

return $query->get();


你是对的,push() 不是将两个集合附加在一起的方法,请改用 concat()

对于您的代码,将 push() 替换为 concat() 并将结果分配给同一变量。

$controls_checklists = $controls_checklists->concat(
    ControlsChecklist::where('macro_process_id', $evaluation_item->macro_process_id)
        ->where('process_id', 'like', $evaluation_item->process_id ? $evaluation_item->process_id : '%')
        ->where('status', 1)
        ->get()
);