diff --git a/app/Livewire/Home/News/Index.php b/app/Livewire/Home/News/Index.php new file mode 100644 index 0000000..448e375 --- /dev/null +++ b/app/Livewire/Home/News/Index.php @@ -0,0 +1,97 @@ +resetPage(); + } + + public function updatedCategory() + { + $this->resetPage(); + } + + public function updatedTag() + { + $this->resetPage(); + } + + public function render() + { + $news = News::published() + ->with(['categories', 'author']) + ->when($this->search, function ($query) { + $query->where('title', 'like', '%'.$this->search.'%') + ->orWhere('content', 'like', '%'.$this->search.'%'); + }) + ->when($this->category, function ($query) { + $query->whereHas('categories', function ($query) { + $query->where('categories.id', $this->category); + }); + }) + ->when($this->tag, function ($query) { + $query->withAnyTags([$this->tag]); + }) + ->latest('published_at') + ->paginate(5) + ->through(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') ?: '-'; + $news->author_name = $news->author?->name ?: 'Admin'; + $news->formatted_views = number_format($news->views).' Views'; + $news->excerpt = str($news->content)->stripTags()->limit(160); + + return $news; + }); + + $categories = Category::active() + ->withCount(['news' => function ($query) { + $query->published(); + }]) + ->orderBy('sort_order') + ->get(); + + $popularPosts = News::published() + ->latest('views') + ->limit(3) + ->get() + ->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.index', [ + 'pageTitle' => 'Berita', + 'pageDescription' => 'Jelajahi berita terbaru seputar purwakarta.', + 'news' => $news, + 'popularPosts' => $popularPosts, + 'categories' => $categories, + 'tags' => Tag::all(), + ]); + } +} diff --git a/app/Livewire/Home/News/Show.php b/app/Livewire/Home/News/Show.php new file mode 100644 index 0000000..d7ee5b5 --- /dev/null +++ b/app/Livewire/Home/News/Show.php @@ -0,0 +1,66 @@ +status !== NewsStatus::PUBLISHED) { + abort(404); + } + + $this->news = $news; + $this->news->increment('views'); + } + + #[Title('Detail Berita')] + public function render() + { + $this->news->load(['categories', '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) + ->whereHas('categories', function ($query) { + $query->whereIn('categories.id', $this->news->categories->pluck('id')); + }) + ->latest('published_at') + ->limit(4) + ->get(); + + if ($relatedNews->isEmpty()) { + $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') ?: '-'; + $news->primary_category = $news->categories->first(); + + return $news; + }); + + return view('livewire.home.news.show', [ + 'relatedNews' => $relatedNews, + ]); + } +} diff --git a/resources/views/components/layouts/app.blade.php b/resources/views/components/layouts/app.blade.php index 10dd61e..a326fa4 100644 --- a/resources/views/components/layouts/app.blade.php +++ b/resources/views/components/layouts/app.blade.php @@ -27,8 +27,12 @@
+