Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shadrix's avatar
Level 12

How to implement a multi rating system?

First of all. Thank you for your time and help. I feel bad asking questions, but I'm stuck.

Currently, I'm trying to implement a rating system. Not a normal one. It's very similar to the Airbnb one, where a User can give the overall rating from 1-5 but also has the option to rate other things like the location/value etc.

In my case, I have an "offer" table. Each offer can be a different type of thing. It can be food, it can be an event, a service etc.

Every offer can be ordered by a user.

Now after buying the offer you can rate it. However, for each type of offer, you have different optional ratings.

For example for the type "food", you can optionally rate the taste, the freshness, the quantity or the value.

I'm not sure how to implement this... my current thought is like this:

List item

Every review consists of the public body text, the optional private text to the seller and the response of the seller (if there is one). And it's overall rating.

One review has one optional review. In the example of food, I would save the additional ratings in JSON.

But again in my last post, you cannot calculate the averages etc very well with it. But I don't want to add for every offer type a special table again.

How would you handle this?

0 likes
8 replies
MikeRees's avatar

I would have a table for Offers and a table for Review Types. Offers can have 1 or Many Review Types, and Review Types can have 1 or Many Offers (so that repetition is cut down). That some types of reviews are optional doesn't need separating them out into another table.

From here I'd create a pivot to attach Review Types to Offers. I'm a little unsure how I'd then go on to attach Reviews to this. Either give the pivot an ID and attach Reviews to that (I feel like this is probably the bad way) or give the Reviews a composite key of Offer and Review Type (unsure if this is supported by Eloquent yet or planned to be). To be honest, I think the second solution is better, and then use a service and the query builder to retrieve the Reviews for an Offer.

1 like
shadrix's avatar
Level 12

@MikeRees thank you for your help. It's hard to for me to understand it still. I tried to "translate" your tips into this diagram, but I think it's weird for me.

enter image description here

so you would add all different kinds of ratings in one table? still confused sorry

MikeRees's avatar
MikeRees
Best Answer
Level 1

I'm not sure how to embed an image in a reply, so I'll just create a link here, but you're slightly off how I pictured it.

What I meant by putting all review types in a single table is having them as rows in the review_types table rather than as columns in the offer_reviews table. It might take a bit of a paradigm shift, but consider the different types of reviews possible to be completely separate reviews for an offer. Then what you consider a single review at the moment to be all reviews made by a single user for a single offer.

1 like
shadrix's avatar
Level 12

@MikeRees OH I see! I will give it a try!

And here is what I use to add a picture:

[![enter image description here][1]][1]
[1]: https://i.imgur.com/Rr9g0BY.png

How did you draw the diagram? Reminds me of a program that I used at school but it was years ago...

shadrix's avatar
Level 12

@MikeRees I need your help again... Hopefully, you are reading this again.

enter image description here

I changed the diagram a bit so that I can add the review bodies.

I implemented it like this, but then it struck me. Why do I need the pivot table, when I could use table "ratings" as my pivot table?

Because let's say I have the id of an "offer review", I can get easily all collections of ratings. And I get all values at the same time.

So I think I could just delete "offer_review_rating_types" or do I miss something?

MikeRees's avatar

You could use just the one pivot table yes. I was encoding some business logic in the design. I was attaching the eligibility of a user to make a certain type of rating via the offer_review_rating_types pivot and then storing the actual ratings in the other pivot. Consider the former the columns for the latter's rows.

You could store that some other way though if you're not a fan of circular relationships.

1 like

Please or to participate in this conversation.