使用 Laravel 集合让我头疼。我有两个收藏:
$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 ...
我有第二个收藏给我一个给定月份的所有日子。
\Illuminate\Support\Collection
( $days
)
#attributes: array:1 [▼
"date" => "2016-02-01 00:00:00"
]
// ... and the rest of the month.
我想将$days
集合与$timesheet
集合合并,以保留集合的值$timesheet
并删除集合中存在的任何重复项$days
。例如。如果已经$timesheets
包含'2016-02-24'
我不想从'2016-02-24'
. $days
我该怎么做呢?