svoydom/app/Http/Controllers/Admin/SliderController.php
2023-05-17 09:57:20 +03:00

73 lines
1.7 KiB
PHP
Executable File

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Requests\SliderRequest;
use Illuminate\Http\Request;
use App\Models\Slider;
use Illuminate\Http\RedirectResponse;
class SliderController extends Controller
{
public function index()
{
$sliders = Slider::latest()->get();
return view('admin.sliders.index', compact('sliders'));
}
public function create()
{
return view('admin.sliders.create');
}
public function store(SliderRequest $request): RedirectResponse
{
$data = $request->validated();
if ($request->hasFile('image')) {
$data['image'] = $this->fileUpload($request->file('image'));
}
Slider::create($data);
return redirect()->route('admin.sliders.index')->with('success', 'Слайдер создан успешно !');
}
public function show($id)
{
//
}
public function edit(Slider $slider)
{
return view('admin.sliders.edit', compact('slider'));
}
public function update(Request $request, Slider $slider)
{
$data = $request->all();
if ($request->hasFile('image')) {
$data['image'] = $this->fileUpload($request->file('image'));
}
$slider->update($data);
return redirect()->route('admin.sliders.index')->with('success', 'Слайдер обновлен успешно !');
}
public function destroy(Slider $slider)
{
$slider->delete();
return redirect()->route('admin.sliders.index')->with('success', 'Слайдер удален успешно !');
}
public function fileUpload($file)
{
$filename = time().'_'.$file->getClientOriginalName();
$file->move(public_path(Slider::FILE_PATH), $filename);
return $filename;
}
}