applicant-site/app/Http/Controllers/NewsController.php

62 lines
1.5 KiB
PHP
Raw Normal View History

2024-03-20 13:09:39 +03:00
<?php
namespace App\Http\Controllers;
use App\Http\Requests\admin\StoreNewsRequest;
use App\Http\Requests\admin\UpdateNewsRequest;
use App\Models\News;
use Illuminate\Support\Facades\Storage;
class NewsController extends Controller
{
public function index()
{
$news = News::all()->sortDesc();
return view('admin.news.index', compact('news'));
}
public function create()
{
return view('admin.news.create');
}
public function store(StoreNewsRequest $request)
{
$validated = $request->validated();
$urlPhoto = Storage::put('public', $request->file('photo'));
$news = new News();
$news->name = $validated['name'];
$news->text = $validated['text'];
$news->photo = Storage::url($urlPhoto);
$news->save();
return redirect()->route('news.index');
}
public function show(News $news)
{
return view('admin.news.show', compact('news'));
}
public function edit(News $news)
{
return view('admin.news.edit', compact('news'));
}
public function update(UpdateNewsRequest $request, News $news)
{
$validated = $request->validated();
$news->name = $validated['name'];
$news->text = $validated['text'];
$news->save();
return redirect()->route('news.index');
}
public function destroy(News $news)
{
$news->delete();
return redirect()->route('news.index');
}
}