forked from eatvio/eatvio-infra
04 10 2023 snapshot
This commit is contained in:
52
app/Livewire/BlogPost/ListView.php
Normal file
52
app/Livewire/BlogPost/ListView.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\BlogPost;
|
||||
|
||||
use Livewire\Component;
|
||||
use Livewire\WithPagination;
|
||||
use App\Models\BlogPost;
|
||||
use App\Models\User;
|
||||
use App\Models\Tag;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class ListView extends Component
|
||||
{
|
||||
use WithPagination;
|
||||
|
||||
public $search = "";
|
||||
public $typeFilter = "all";
|
||||
public $tag = '';
|
||||
public $author;
|
||||
|
||||
protected $queryString = [
|
||||
'search' => ['except' => ''],
|
||||
'typeFilter' => ['except' => 'all'],
|
||||
'tag' => ['except' => ''],
|
||||
'author' => ['except' => '']
|
||||
];
|
||||
|
||||
public function render()
|
||||
{
|
||||
if ($this->author) {
|
||||
$author = $this->author;
|
||||
$user = User::find($author);
|
||||
$posts = $user ? $user->author_of_blog_post()->distinct() : [];
|
||||
} elseif($this->tag) {
|
||||
$tagId = Str::of($this->tag)->split('/-/')->first();
|
||||
$posts = Tag::find($tagId)->blogPosts();
|
||||
} elseif ($this->search && mb_strlen($this->search) > 2) {
|
||||
$posts = BlogPost::where('title', 'rlike', $this->search);
|
||||
} else {
|
||||
$posts = (new BlogPost);
|
||||
}
|
||||
|
||||
if($this->typeFilter == 'user') {
|
||||
$posts = $posts->whereRelation('author', 'is_consultant', false);
|
||||
}
|
||||
if ($this->typeFilter == 'consultant') {
|
||||
$posts = $posts->whereRelation('author', 'is_consultant', true);
|
||||
}
|
||||
|
||||
return view('livewire.blog-post.list-view', ['blogPosts' => $posts->orderBy('created_at', 'desc')->paginate(10)]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user