Search code examples
phplaraveleloquent

Append to already fetched collection - eloquent


I have 2 methods like:

public function personSearch(Request $request)
    {
        $day = $request['day'];
        $people = Person::where('day', $day)->get();
        Session::put('people', $people);
        return $people;
    }

And another one to output to PDF:

public function generatePdf(Request $request)
{
    $people     = Session::get('people');
    if ($request['personalId'] == "true") {
        $people = $people->where('uid', 'like', 9999)->get();
    }
}

Is there any possibility to append additional condition on already created collection of data?


Solution

  • you could try to merge the two collections like

    $original = new Collection(['foo']);
    $latest = new Collection(['bar']);
    $merged = $original->merge($latest);
    

    or you could change the two collections into arrays ->toArray() and user array_merge method

    I hope I understood your question correctly