136 lines
4.3 KiB
PHP
136 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Livewire;
|
|
|
|
use App\Models\Post;
|
|
use Illuminate\Support\Facades\App;
|
|
use Livewire\Component;
|
|
|
|
class MainPost extends Component
|
|
{
|
|
public $type;
|
|
public bool $sticky = false;
|
|
public bool $random = false;
|
|
public bool $latest = false;
|
|
public $fallbackTitle = null;
|
|
public $fallbackDescription = null;
|
|
|
|
public function mount($type, $sticky = null, $random = null, $latest = null, $fallbackTitle = null, $fallbackDescription = null)
|
|
{
|
|
$this->type = $type;
|
|
$this->fallbackTitle = $fallbackTitle;
|
|
$this->fallbackDescription = $fallbackDescription;
|
|
|
|
if ($sticky) {
|
|
$this->sticky = true;
|
|
}
|
|
if ($random) {
|
|
$this->random = true;
|
|
}
|
|
if ($latest) {
|
|
$this->latest = true;
|
|
}
|
|
}
|
|
|
|
|
|
public function render()
|
|
{
|
|
// Sticky post
|
|
if ($this->sticky) {
|
|
$locale = App::getLocale();
|
|
|
|
$posts = Post::with([
|
|
'category',
|
|
'images' => function ($query) {
|
|
$query->select('images.id', 'caption', 'path');
|
|
},
|
|
'translations' => function ($query) use ($locale) {
|
|
$query->where('locale', $locale)
|
|
->orderBy('created_at', 'desc')
|
|
->limit(3);
|
|
}
|
|
])
|
|
->whereHas('category', function ($query) {
|
|
$query->where('type', $this->type);
|
|
})
|
|
->whereHas('translations', function ($query) use ($locale) {
|
|
$query->where('locale', $locale)
|
|
->whereDate('from', '<=', now())
|
|
->where(function ($query) {
|
|
$query->whereDate('till', '>', now())->orWhereNull('till');
|
|
})
|
|
->orderBy('updated_at', 'desc');
|
|
})
|
|
->orderBy('created_at', 'desc')
|
|
->limit(3)
|
|
->first();
|
|
}
|
|
|
|
|
|
// Random post
|
|
if ($this->random) {
|
|
$locale = App::getLocale();
|
|
|
|
$posts = Post::with([
|
|
'category',
|
|
'translations' => function ($query) use ($locale) {
|
|
$query->where('locale', $locale)
|
|
->orderBy('created_at', 'desc')
|
|
->limit(1);
|
|
}
|
|
])
|
|
->whereHas('category', function ($query) {
|
|
$query->where('type', $this->type);
|
|
})
|
|
->whereHas('translations', function ($query) use ($locale) {
|
|
$query->where('locale', $locale)
|
|
->whereDate('from', '<=', now())
|
|
->where(function ($query) {
|
|
$query->whereDate('till', '>', now())->orWhereNull('till');
|
|
})
|
|
->orderBy('updated_at', 'desc');
|
|
})
|
|
->inRandomOrder() // This replaces the orderBy() method
|
|
->first();
|
|
}
|
|
|
|
// Latest post
|
|
if ($this->latest) {
|
|
$locale = App::getLocale();
|
|
|
|
$posts = Post::with([
|
|
'category',
|
|
'translations' => function ($query) use ($locale) {
|
|
$query->where('locale', $locale)
|
|
->orderBy('created_at', 'desc')
|
|
->limit(1);
|
|
}
|
|
])
|
|
->whereHas('category', function ($query) {
|
|
$query->where('type', $this->type);
|
|
})
|
|
->whereHas('translations', function ($query) use ($locale) {
|
|
$query->where('locale', $locale)
|
|
->whereDate('from', '<=', now())
|
|
->where(function ($query) {
|
|
$query->whereDate('till', '>', now())->orWhereNull('till');
|
|
})
|
|
->orderBy('updated_at', 'desc');
|
|
})
|
|
->orderBy('created_at', 'desc')
|
|
->first();
|
|
}
|
|
|
|
|
|
$image = null;
|
|
if ($posts && $posts->hasMedia('*')) {
|
|
$image = $posts->getFirstMediaUrl('*', 'half_hero');
|
|
}
|
|
|
|
return view('livewire.main-post', [
|
|
'posts' => $posts,
|
|
'image' => $image,
|
|
]);
|
|
}
|
|
}
|