simedkom/app/Livewire/Home/News/Show.php

57 lines
1.8 KiB
PHP

<?php
namespace App\Livewire\Home\News;
use App\Enums\NewsStatus;
use App\Models\News;
use Livewire\Attributes\Title;
use Livewire\Component;
class Show extends Component
{
public News $news;
public function mount(News $news)
{
if ($news->status !== NewsStatus::PUBLISHED) {
abort(404);
}
$this->news = $news;
$this->news->increment('views');
}
#[Title('Detail Berita')]
public function render()
{
$this->news->load(['author', 'tags']);
// Format main news
$this->news->thumbnail = $this->news->getFirstMediaUrl('news') ?: 'https://images.pexels.com/photos/12199409/pexels-photo-12199409.jpeg';
$this->news->formatted_date = $this->news->published_at?->translatedFormat('d F Y') ?: '-';
$this->news->author_name = $this->news->author?->name ?: 'Admin';
$this->news->author_avatar = 'https://ui-avatars.com/api/?name='.urlencode($this->news->author?->name ?: 'A');
$this->news->formatted_views = number_format($this->news->views).' Views';
$relatedNews = News::published()
->where('id', '!=', $this->news->id)
->latest('published_at')
->limit(4)
->get();
// Format related news
$relatedNews = $relatedNews->map(function ($news) {
$news->thumbnail = $news->getFirstMediaUrl('news') ?: 'https://images.pexels.com/photos/12199409/pexels-photo-12199409.jpeg';
$news->formatted_date = $news->published_at?->translatedFormat('d F Y') ?: '-';
return $news;
});
return view('livewire.home.news.show', [
'pageTitle' => 'Detail Berita',
'pageDescription' => 'Detail Berita',
'relatedNews' => $relatedNews,
]);
}
}