новости + переработка текстов
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Concerns\BuildsPortalProps;
|
||||
use App\Models\News;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
|
||||
class NewsController extends Controller
|
||||
{
|
||||
use BuildsPortalProps;
|
||||
|
||||
public function index(Request $request): Response
|
||||
{
|
||||
$articles = News::query()
|
||||
->published()
|
||||
->latest('created_at')
|
||||
->latest('id')
|
||||
->paginate(9)
|
||||
->withQueryString()
|
||||
->through(fn (News $item): array => $this->summary($item));
|
||||
|
||||
return Inertia::render('News/Index', [
|
||||
...$this->portalProps($request),
|
||||
'articles' => $articles,
|
||||
]);
|
||||
}
|
||||
|
||||
public function show(Request $request, News $news): Response
|
||||
{
|
||||
abort_unless($news->is_published, 404);
|
||||
|
||||
$moreNews = News::query()
|
||||
->published()
|
||||
->where('id', '!=', $news->id)
|
||||
->latest('created_at')
|
||||
->latest('id')
|
||||
->limit(3)
|
||||
->get()
|
||||
->map(fn (News $item): array => $this->summary($item))
|
||||
->values()
|
||||
->all();
|
||||
|
||||
return Inertia::render('News/Show', [
|
||||
...$this->portalProps($request),
|
||||
'article' => [
|
||||
...$this->summary($news),
|
||||
'body' => $news->body,
|
||||
'canonical_url' => route('news.show', $news),
|
||||
'back_url' => route('news.index'),
|
||||
],
|
||||
'moreNews' => $moreNews,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function summary(News $news): array
|
||||
{
|
||||
return [
|
||||
'id' => $news->id,
|
||||
'title' => $news->title,
|
||||
'excerpt' => Str::limit(trim(strip_tags($news->body)), 200),
|
||||
'date' => $news->created_at?->locale(app()->getLocale())->isoFormat('D MMMM YYYY'),
|
||||
'image_url' => $news->image_url,
|
||||
'url' => route('news.show', $news),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user