validate([ 'title' => 'nullable|string|max:255', 'subtitle' => 'nullable|string|max:255', 'description' => 'nullable|string', 'menu_title_1' => 'nullable|string|max:255', 'menu_description_1' => 'nullable|string', 'menu_title_2' => 'nullable|string|max:255', 'menu_description_2' => 'nullable|string', 'menu_title_3' => 'nullable|string|max:255', 'menu_description_3' => 'nullable|string', 'menu_title_4' => 'nullable|string|max:255', 'menu_description_4' => 'nullable|string', 'image' => 'nullable|image|max:2048' ]); $data = MenuPage::first() ?? new MenuPage; $data->fill($request->except('image', 'show_image')); $data->show_image = $request->has('show_image'); if ($request->hasFile('image')) { if ($data->image && file_exists(public_path($data->image))) { unlink(public_path($data->image)); } $imageName = time() . '.' . $request->image->getClientOriginalExtension(); $request->image->move(public_path('uploads/menu'), $imageName); $data->image = 'uploads/menu/' . $imageName; } elseif (!$request->has('show_image') && $data->image) { if (file_exists(public_path($data->image))) { unlink(public_path($data->image)); } $data->image = null; } $data->save(); return redirect()->back()->with('success', 'Menu Page saved successfully!'); } }