File: /home/armgroup/libs/app/Http/Livewire/Frontend/Product/Comment/Feedback.php
<?php
namespace App\Http\Livewire\Frontend\Product\Comment;
use App\Enums\FeedbackType;
use App\Http\Livewire\General;
use App\Models\Comment;
use App\Models\Feedback as FeedbackClass;
use Livewire\Component;
class Feedback extends Component
{
use General;
public Comment $comment;
public $type;
public $like;
public $dislike;
public function mount()
{
$this->like = $this->comment->feedback->where('type', FeedbackType::LIKE)->count();
$this->dislike = $this->comment->feedback->where('type', FeedbackType::DISLIKE)->count();
if (auth()->check()) {
$this->type = FeedbackClass::where('user_id', auth()->id())
->where('feedbackable_id', $this->comment->id)
->where('feedbackable_type', Comment::class)
->first()
?->type;
}
}
public function feedback($type)
{
if (! auth()->check()) {
$this->ajaxDoneMessage('برای اعمال فیدبک باید وارد سایت شوید', 'error');
return;
}
if (! in_array($type, FeedbackType::asArray())) {
return;
}
FeedbackClass::updateOrCreate([
'user_id' => auth()->id(),
'feedbackable_id' => $this->comment->id,
'feedbackable_type' => Comment::class,
], ['type' => $type]);
$this->type = $type;
$feedbacks = $this->comment->feedback()->get();
$this->like = $feedbacks->where('type', FeedbackType::LIKE)->count();
$this->dislike = $feedbacks->where('type', FeedbackType::DISLIKE)->count();
$this->ajaxDoneMessage();
}
public function render()
{
return view('livewire.frontend.product.comment.feedback');
}
}