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');
}
My Answer:
To display the total ratings in stars in your blade view, you can use a loop to iterate over the average rating and display the corresponding number of filled stars. Here's an example of how you can achieve this:
html
In this example, we are using Font Awesome icons to represent the stars. The first loop calculates the average rating of the product and displays the filled stars up to that rating. The second loop displays the empty stars for the remaining rating slots.
Make sure to replace `$product->ratings()->avg('rating')` with the actual method to calculate the average rating of the product in your application.
You can place this code snippet in your blade view file where you want to display the total ratings in stars.
Rate this post
3 of 5 based on 1012 votesComments