BusterX
BusterX

Reputation: 125

Merging two Laravel collections

My head hurts from working with Laravel collections. I have two collections:

    $dt = Carbon::now();
    $days = new Collection([]);

    /**
     * Create a calender month
     */
    for ($day = 1; $day <= $dt->daysInMonth; $day++) {
        $date = Carbon::create($dt->year, $dt->month, $day)->toDateString();
        $days->push(new Timesheet([
            'date' => $date,
        ]));
    }

    /**
     * Get all timesheets for user
     */
    $timesheets = Timesheet::where('user_id', $this->user->id)
        ->get();

\Illuminate\Database\Eloquent\Collection ($timesheets)

#attributes: array:5 [▼
    "id" => "1"
    "user_id" => "1"
    "date" => "2016-02-22 22:05:01"
    "created_at" => "2016-02-22 22:05:01"
    "updated_at" => "2016-02-22 22:05:01"
  ]
  // ... one or more ...

I have second collection giving me all days for a given month.

\Illuminate\Support\Collection ($days)

#attributes: array:1 [▼
    "date" => "2016-02-01 00:00:00"
]
// ... and the rest of the month.

I want to merge the $days collection with the $timesheet collection preserving the values of the $timesheet collection and removing any duplicates present in the $days collection. E. g. if $timesheets already contains '2016-02-24' I do not want to merge '2016-02-24' from $days. How do I do this?

Upvotes: 5

Views: 3455

Answers (3)

Mike Miller
Mike Miller

Reputation: 3129

OK have a go with this. Logic should pretty much work out but obv didnt have access to your Timesheet class..

$days = new Collection([]);

//basically the same structure i think
$timesheets = new Collection([new Collection(['date'=>'2016-02-23','created_at'=>'2016-02-23 14:12:34']),new Collection(['date'=>'2016-02-28','created_at'=>'2016-02-23 14:15:36'])]);

$dt = Carbon::now();

for ($day = 1; $day <= $dt->daysInMonth; $day++) {

    $date = Carbon::create($dt->year, $dt->month, $day)->format('Y-m-d');

    //filter your timesheets and see if there is one for this day
    $timesheet = $timesheets->filter(function($timesheet) use($date){return $timesheet->get('date')==$date;});

    if(!$timesheet->isEmpty()){
        //if there is a timesheet for today then add it to your $days collection
        $days->push($timesheet);
    }else{
        //otherwise just stick in the date
        $days->push(new Collection([
            'date' => $date,
        ]));
   }
}

//voila!
dd($days);

Upvotes: 1

Dwijen
Dwijen

Reputation: 590

I am not sure why the $merged = $timesheets->merge($days); merged only the last item. Maybe someone else can shed some light on it.

But until there's a better solution, you can do this -

$merged = array_merge($timesheets->toArray(), $days->toArray());

Hope this helps.

Upvotes: 2

Jeff Lambert
Jeff Lambert

Reputation: 24661

Use merge:

$collection1 = Model1::all();
$collection2 = Model2::all();
$mergedCollection = $collection1->merge($collection2);

Documentation

The documentation talks about using it with arrays, but looking at the method signature it will take mixed arguments. Testing it on a local install of a Laravel 4 project worked for me.

Upvotes: 5

Related Questions