mehrdad70's avatar

Implement a star rating system in Laravel

Hi everyone I want to implement a star rating system for my products, but I do not have the experience to do so. Training for this topic, if any, please give me the link. Thank you.

0 likes
5 replies
mehrdad70's avatar

Is this using a package? I want to implement this without using the package, do you suggest another way?

jlrdw's avatar

If it's a GitHub package, that means you can look over the code and get ideas of how to do it yourself.

you don't have to use the package.

Edit: you could wrap star images in a styled division or whatever element.

Jeffrey does have videos where he uses images inside of divisions.

zerosAndOnes's avatar

This is how I did mine, once upon a time.

  1. I created model and migration for rating (with: rating, rating title, rating body, foreign id to user and foreign id to product)
  2. In products model, I just created a function called ratingAverage to calculate the sum/average rating of a products

e.g

public function ratingAverage()
{
    return $this->rating->avg('rating');
}

Lastly, use sass to to style the stars as you can pass in the value of the rating dynamically from the blade view by calling the ratingAverage method on a product you are displaying.

You can style the stars whatever way you choose, but I used sass.

Goodluck!

Please or to participate in this conversation.