forked from eatvio/eatvio-infra
39 lines
788 B
PHP
39 lines
788 B
PHP
<?php
|
|
|
|
namespace App\Livewire\Recipe;
|
|
|
|
use App\Models\Recipe;
|
|
use Livewire\Component;
|
|
use Illuminate\View\View;
|
|
|
|
class Like extends Component
|
|
{
|
|
public Recipe $recipe;
|
|
|
|
public bool $isIconOnly = false;
|
|
|
|
public function mount(Recipe $recipe): void
|
|
{
|
|
$this->recipe = $recipe;
|
|
}
|
|
|
|
public function render(): View
|
|
{
|
|
return view('livewire.recipe.like');
|
|
}
|
|
|
|
public function like(): void
|
|
{
|
|
if ($this->recipe->isLiked()) {
|
|
$this->recipe->removeLike();
|
|
} elseif (auth()->user()) {
|
|
$this->recipe->likes()->create([
|
|
'user_id' => auth()->id(),
|
|
'blog_post_id' => $this->recipe->id,
|
|
]);
|
|
}
|
|
|
|
$this->dispatch('liked')->to('recipe.like-count');
|
|
}
|
|
}
|