Files
eatvio-infra/app/Livewire/Recipe/Comment/Create.php
2023-12-28 17:07:09 +03:00

53 lines
1.0 KiB
PHP

<?php
namespace App\Livewire\Recipe\Comment;
use App\Models\Recipe;
use App\Models\RecipeComment;
use App\Models\User;
use Livewire\Component;
class Create extends Component
{
public Recipe $recipe;
public User $user;
public $body = '';
public $grade = 0;
public $isChecked = false;
public function render()
{
return view('livewire.recipe.comment.create');
}
public function toggle()
{
$this->isChecked = ! $this->isChecked;
}
public function save()
{
$this->validate([
'body' => 'required|min:3',
'grade' => 'required',
]);
$comment = new RecipeComment();
$comment->grade = $this->grade;
$comment->body = $this->body;
$comment->recipe_id = $this->recipe->id;
$comment->user_id = $this->user->id;
$comment->save();
$this->reset(['body', 'grade']);
$this->toggle();
$this->dispatch('commentAdded')->to('recipe.comment.show');
}
}