ownedStoreWithHero($request, $slug, $sectionId); $validated = $request->validate([ 'image' => ['nullable', 'string', 'regex:'.HeroImages::PATTERN], 'variant' => ['required', Rule::in(PlanSchema::SECTIONS['hero'])], ]); $image = $validated['image'] ?? null; if ($image !== null && HeroImages::unknown($store, [$image]) !== []) { return back()->withErrors(['image' => __('Choose one of your photos.')]); } return $this->save($store, $validator, $sectionId, $image, $validated['variant']); } /** * Upload a new photo and use it in this hero. A text-only hero switches to "photo beside text" so it shows. */ public function upload(Request $request, PlanValidator $validator, string $slug, string $sectionId): RedirectResponse { $store = $this->ownedStoreWithHero($request, $slug, $sectionId); $request->validate([ 'photo' => ['required', 'image', 'mimes:jpg,jpeg,png,webp', 'max:6144', 'dimensions:min_width=600,min_height=400'], ], [ 'photo.max' => __('The photo must be 6 MB or smaller.'), 'photo.mimes' => __('Use a JPG, PNG or WebP photo.'), 'photo.dimensions' => __('Use a photo at least 600 pixels wide and 400 high, so it stays sharp.'), ]); $image = $store->images()->create(['path' => $request->file('photo')->store("stores/{$store->id}", 'public')]); $current = collect(HeroImages::sections($store->plan ?? []))->firstWhere('section_id', $sectionId); $variant = ($current['variant'] ?? 'centered') === 'centered' ? 'split' : $current['variant']; return $this->save($store, $validator, $sectionId, $image->reference(), $variant); } private function save(Store $store, PlanValidator $validator, string $sectionId, ?string $image, string $variant): RedirectResponse { $store->plan = $validator->validate(HeroImages::update($store->plan ?? [], $sectionId, $image, $variant)); $store->save(); Inertia::flash('toast', ['type' => 'success', 'message' => $store->isPublished() ? __('Saved. Click "Publish changes" to show it on your live store.') : __('Hero saved.')]); return to_route('stores.show', $store->slug); } private function ownedStoreWithHero(Request $request, string $slug, string $sectionId): Store { $store = $request->user()->stores()->where('slug', $slug)->firstOrFail(); abort_if($store->plan === null, 404); abort_unless(in_array($sectionId, array_column(HeroImages::sections($store->plan), 'section_id'), true), 404); return $store; } }