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'),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,12 +3,18 @@
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class Slider extends Model
|
||||
{
|
||||
protected $fillable = ['title', 'description', 'url','action'];
|
||||
protected $fillable = ['title', 'description', 'url', 'action', 'image_id'];
|
||||
|
||||
protected $casts = [
|
||||
'action' => 'array', // auto convert JSON to array
|
||||
];
|
||||
|
||||
public function image(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Image::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('sliders', function (Blueprint $table) {
|
||||
$table->foreignId('image_id')->nullable()->after('id')
|
||||
->constrained('images')->nullOnDelete();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('sliders', function (Blueprint $table) {
|
||||
$table->dropForeign(['image_id']);
|
||||
$table->dropColumn('image_id');
|
||||
});
|
||||
}
|
||||
};
|
||||
+2
-1
@@ -194,7 +194,8 @@
|
||||
Route::get('/slider', [SliderController::class, 'index']); // list all sliders
|
||||
Route::post('/slider', [SliderController::class, 'store']); // create slider
|
||||
Route::get('/slider/{id}', [SliderController::class, 'show']); // get single slider
|
||||
Route::put('/slider/{id}', [SliderController::class, 'update']); // update slider
|
||||
Route::put('/slider/{id}', [SliderController::class, 'update']); // update slider (json)
|
||||
Route::post('/slider/{id}', [SliderController::class, 'update']); // update slider (multipart: image upload)
|
||||
Route::delete('/slider/{id}', [SliderController::class, 'destroy']); // delete slider
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user