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?
collections
at all. – Liatrice$timesheets->merge($days);
Can you update the Result that you are getting with the merge? – Macurnew Collection([])
, does the helper functioncollect([])
fix your issue? – Apacecollect()
does is return anew Collection([])
. – Seals