Fokwa Best
Fokwa Best

Reputation: 3515

How to Append Laravel Collection Query Results

I have the query below:

$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'));

On my view, I have:

@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

However, when I open the page I get error message

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

For this line

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

I think that this is not working due to the way I am attempting to merge the collections query above. How can I solve this?

Upvotes: 0

Views: 1577

Answers (4)

Anurat Chapanond
Anurat Chapanond

Reputation: 2987

You are right that push() is not the way to append two collections together, use concat() instead.

For your code, replace push() with concat() and assign the result to the same variable.

$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()
);

Upvotes: 1

Elfeqy
Elfeqy

Reputation: 61

you don't have to collect the data in a new collection variable.

try this:

$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();


Upvotes: 0

You will not succeed on looping because your variable is returning a collection of collections.

if you want you can try this.

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);
  }
}

Upvotes: 0

Madhusudan M R
Madhusudan M R

Reputation: 18

First dd($controls_checklist); and verify that in the place of $controls_checklist->id the id actually exists. If not change the foreach loop statement.

Coming to foreach loop, try using the model with join or innerjoin to manage the query result

Thank you

Upvotes: 0

Related Questions