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

64 lines
1.6 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();
2024-06-11 10:37:39 +03:00
$urlPhoto = Storage::put('public', $request->file('photo'));
2024-03-20 13:09:39 +03:00
$news->name = $validated['name'];
$news->text = $validated['text'];
2024-06-11 10:37:39 +03:00
$news->photo = Storage::url($urlPhoto);
2024-03-20 13:09:39 +03:00
$news->save();
return redirect()->route('news.index');
}
public function destroy(News $news)
{
$news->delete();
return redirect()->route('news.index');
}
}