forked from eatvio/eatvio-infra
same at 22.05.2023
This commit is contained in:
36
app/Http/Livewire/BlogPost/Like.php
Normal file
36
app/Http/Livewire/BlogPost/Like.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\BlogPost;
|
||||
|
||||
use Livewire\Component;
|
||||
use App\Models\BlogPost;
|
||||
|
||||
class Like extends Component
|
||||
{
|
||||
public BlogPost $blogPost;
|
||||
public $isIconOnly = false;
|
||||
|
||||
public function mount(BlogPost $blogPost)
|
||||
{
|
||||
$this->blogPost = $blogPost;
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.blog-post.like');
|
||||
}
|
||||
|
||||
public function like(): void
|
||||
{
|
||||
if ($this->blogPost->isLiked()) {
|
||||
$this->blogPost->removeLike();
|
||||
} elseif (auth()->user()) {
|
||||
$this->blogPost->likes()->create([
|
||||
'user_id' => auth()->id(),
|
||||
'blog_post_id' => $this->blogPost->id
|
||||
]);
|
||||
}
|
||||
|
||||
$this->emitTo('blog-post.like-count', 'liked');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user