Initial commit
This commit is contained in:
141
app/Http/Livewire/SidePost.php
Normal file
141
app/Http/Livewire/SidePost.php
Normal file
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use App\Models\Post;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Livewire\Component;
|
||||
|
||||
class SidePost extends Component
|
||||
{
|
||||
public $type;
|
||||
public bool $sticky = false;
|
||||
public bool $random = false;
|
||||
public bool $latest = false;
|
||||
public $fallbackTitle = null;
|
||||
public $fallbackDescription = null;
|
||||
public bool $alwaysShowFull = false;
|
||||
|
||||
public function mount($type, $sticky = null, $random = null, $latest = null, $fallbackTitle = null, $fallbackDescription = null, $alwaysShowFull = false)
|
||||
{
|
||||
$this->type = $type;
|
||||
$this->fallbackTitle = $fallbackTitle;
|
||||
$this->fallbackDescription = $fallbackDescription;
|
||||
|
||||
if ($sticky) {
|
||||
$this->sticky = true;
|
||||
}
|
||||
if ($random) {
|
||||
$this->random = true;
|
||||
}
|
||||
if ($latest) {
|
||||
$this->latest = true;
|
||||
}
|
||||
if ($alwaysShowFull) {
|
||||
$this->alwaysShowFull = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function render()
|
||||
{
|
||||
$post = null;
|
||||
|
||||
// Sticky post
|
||||
if ($this->sticky) {
|
||||
$locale = App::getLocale();
|
||||
|
||||
$post = 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();
|
||||
|
||||
$post = 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();
|
||||
|
||||
$post = 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 ($post && $post->hasMedia('*')) {
|
||||
$image = $post->getFirstMediaUrl('*', 'half_hero');
|
||||
}
|
||||
|
||||
return view('livewire.side-post', [
|
||||
'post' => $post,
|
||||
'image' => $image,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user