feat: add slider image
This commit is contained in:
@@ -4,13 +4,16 @@
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Slider;
|
||||
use App\Traits\HandlesImageUpload;
|
||||
|
||||
class SliderController extends Controller
|
||||
{
|
||||
use HandlesImageUpload;
|
||||
|
||||
public function index()
|
||||
{
|
||||
return response()->json([
|
||||
'data' => Slider::all()
|
||||
'data' => Slider::with('image')->get()
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -20,20 +23,25 @@ public function store(Request $request)
|
||||
'title' => 'required|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
'url' => 'nullable|string|max:500',
|
||||
'action' => 'nullable|array',
|
||||
'action' => 'nullable|array',
|
||||
'image_id' => 'nullable|exists:images,id',
|
||||
'image' => 'nullable|file|extensions:jpg,jpeg,png,gif,webp,bmp,svg|max:8192',
|
||||
]);
|
||||
|
||||
// An uploaded image file takes precedence over a provided image_id.
|
||||
$data['image_id'] = $this->uploadedImageId($request) ?? ($data['image_id'] ?? null);
|
||||
|
||||
$slider = Slider::create($data);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Slider created successfully',
|
||||
'data' => $slider
|
||||
'data' => $slider->load('image'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$slider = Slider::findOrFail($id);
|
||||
$slider = Slider::with('image')->findOrFail($id);
|
||||
return response()->json($slider);
|
||||
}
|
||||
|
||||
@@ -46,13 +54,20 @@ public function update(Request $request, $id)
|
||||
'description' => 'sometimes|string',
|
||||
'url' => 'sometimes|string|max:500',
|
||||
'action' => 'nullable|array',
|
||||
'image_id' => 'nullable|exists:images,id',
|
||||
'image' => 'nullable|file|extensions:jpg,jpeg,png,gif,webp,bmp,svg|max:8192',
|
||||
]);
|
||||
|
||||
// An uploaded image file takes precedence over a provided image_id.
|
||||
if (($uploadedImageId = $this->uploadedImageId($request)) !== null) {
|
||||
$data['image_id'] = $uploadedImageId;
|
||||
}
|
||||
|
||||
$slider->update($data);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Slider updated successfully',
|
||||
'data' => $slider
|
||||
'data' => $slider->load('image'),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user