ldc-groupe/app/View/Components/Front/News/NewsNextEventComponent.php

55 lines
1.7 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\View\Components\Front\News;
use App\Models\News;
use App\Models\NewsTag;
use App\Models\NewsCategory;
use Illuminate\View\Component;
class NewsNextEventComponent extends Component
{
2026-02-12 17:14:08 +01:00
/** @var \Illuminate\Support\Collection<int, \App\Models\News> $nextEvents List of published news. */
public $nextEvents;
/**
* Create a new component instance.
*/
public function __construct()
{
2026-02-12 17:14:08 +01:00
/** @var \App\Models\NewsCategory|null $newsCategory */
$newsCategory = NewsCategory::getModelsCached()
->filter(fn($category) => $category->getTranslation('slug', 'fr') === "actualites")
->first();
2026-02-12 17:14:08 +01:00
/** @var \App\Models\NewsTag|null $eventTag */
$eventTag = NewsTag::getTagsByCategoryInCurrentLocale($newsCategory->slug)
2026-02-12 17:14:08 +01:00
->filter(
fn(\Illuminate\Database\Eloquent\Model $tag) => $tag->getTranslation('slug', 'fr') === "actualites-rh"
2026-02-12 17:14:08 +01:00
)
->first();
$eventTagId = $eventTag ? $eventTag->id : null;
$this->nextEvents = News::getPublishedNewsByCategoryInCurrentLocale($newsCategory->slug)
->filter(function ($news) use ($eventTagId) {
return $news->tags->contains(
2026-02-12 17:14:08 +01:00
fn($tag) => $eventTagId === $tag->id
);
})
->sortByDesc('published_at')
->take(3);
}
/**
* Get the view / contents that represent the component.
2026-02-10 15:43:47 +01:00
*
* @return \Illuminate\Contracts\View\View
*/
2026-02-10 15:43:47 +01:00
public function render(): \Illuminate\Contracts\View\View
{
return view('components.front.news.news-next-event-component', [
'nextEvents' => $this->nextEvents
]);
}
}