The rating module is working well, number of ratings are also showing but I want to display total ratings in stars but I don’t know how to write in blade view.
Livewire/ProductRating.php
public $rating; public $comment; public $currentId; public $product; public $hideForm; protected $rules = [ 'rating' => ['required', 'in:1,2,3,4,5'], 'comment' => 'required', ]; public function render() { $comments = Rating::where('product_id', $this->product->id)->where('status', 1)->with('user')->get(); return view('livewire.product-ratings', compact('comments')); } public function mount() { if (auth()->user()) { $rating = Rating::where('user_id', auth()->user()->id)->where('product_id', $this->product->id)->first(); if (!empty($rating)) { $this->rating = $rating->rating; $this->comment = $rating->comment; $this->currentId = $rating->id; } } return view('livewire.product-ratings'); }
Advertisement
Answer
Assuming that your Rating
model has a property on it called rating
, you could do the following to get the average rating for a product with the given id
:
$average = Rating::where('product_id', $id)->avg('rating');