timeslotSettings; if ($timeslotSettings->count() === 0) { // Aucun settings pour ce magasin. $timeslotSettings = $this->makeCreneauxSettings($shop); } return view('back::pages.timeslots.settings', [ 'timeslotSettings' => $timeslotSettings, 'shopModel' => $shop ]); } /** * Retrieve creneaux settings * * @param \App\Models\Shop $shop * @return mixed */ private function makeCreneauxSettings(Shop $shop) { return DB::transaction(function () use ($shop) { for ($i = 0; $i < 7; $i++) { $dayset = new TimeslotSetting(); $dayset->day_index = \strval($i); $dayset->shop()->associate($shop); $dayset->saveOrFail(); } return $shop->timeslotSettings; }); } /** * save settings * * @param \App\Http\Requests\Bo\Timeslots\UpdateTimeslotRequest $request * @param \App\Models\Shop $shop * @return \Illuminate\Http\RedirectResponse */ public function updateSettings(UpdateTimeslotRequest $request, Shop $shop) { return DB::transaction(function () use ($request, $shop) { $validated = $request->validated()['plages']; foreach (WeekDay::toArray() as $weekDay) { $setting = TimeslotSetting::where('day', $weekDay) ->whereHas('shop', function (Builder $q) use ($shop) { $q->where('id', $shop->id); })->firstOrFail(); $setting->fill($validated[$weekDay->value]); $setting->saveOrFail(); } return redirect()->route('bo.timeslots.settings', $shop) ->with('success', 'Modifications enregistrées !'); }); } }