<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Models\Slider;

class SliderController extends Controller
{
    public function store(Request $request)
    {
        // Validation etc...

        $slider = new Slider();
        $slider->title = $request->title;
        // ... other fields ...

        if ($request->hasFile('image')) {
            $image = $request->file('image');

            $name = time() . '_' . uniqid() . '.' . $image->getClientOriginalExtension();

            // public/storage/sliders/filename.jpg
            Storage::disk('public')->putFileAs('sliders', $image, $name);

            $slider->image = 'sliders/' . $name;
        }

        $slider->save();

        return redirect()->back()->with('success', 'Slider created successfully');
    }
}
