Laravel コレクションの操作で頭が痛くなります。私は2つのコレクションを持っています:
$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 ...
特定の月のすべての日を提供する 2 番目のコレクションがあります。
\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