/** * @param array $data * @return Exhibition */ public function createAndSave(array $data) { $exhibition = isset($data['id']) ? Exhibition::find($data['id']) : new Exhibition(); if (isset($data['exhibition_film']['id'])) { $exhibitionFilm = ExhibitionFilm::find($data['exhibition_film']['id']); } else { $exhibitionFilm = new ExhibitionFilm(); } $exhibitionFilm->film_id = $data['exhibition_film']['film']['id']; $exhibitionFilm->save(); $exhibition->exhibitionFilm()->associate($exhibitionFilm); $exhibition->notes = isset($data['notes']) ? $data['notes'] : ''; if (isset($data['icon']['id'])) { $exhibition->type_id = $data['icon']['id']; } else { $exhibition->type_id = null; } $exhibition->save(); Schedule::where('exhibition_id', $exhibition->id)->delete(); $schedules = array_reduce($data['schedules'], function ($schedules, $rawSchedule) { $schedule = new Schedule(); $schedule->entry = $rawSchedule['entry']; $schedule->auditorium_id = $rawSchedule['auditorium']['id']; $schedules[] = $schedule; return $schedules; }, []); $exhibition->schedules()->saveMany($schedules); return Exhibition::findOrFail($exhibition->id); }
public function findByCycle(Type $cycle, Carbon $since, Carbon $until, $limit = self::EXHIBITIONS_FOR_PAGE) { $dateInterval = [$since, $until]; $builder = Exhibition::where('type_id', $cycle->getId())->whereHas('schedules', function (Builder $query) use($dateInterval) { $query->whereBetween('entry', $dateInterval); }); $exhibitions = $this->addRelationships($builder, $limit); return $exhibitions; }