The introduction of a rating system in Laravel

I already work in a web application in Laravel that needs a rating system between users. So, I read the Laravel documentation and some posts here and there, but I keep wondering what is the best way to accomplish what I want to do. I can not solve the following two options.

My current tables:

users (id) | tariffs (id, sender, comment, bid) | user_rate (id, user_id, rate_id)

My current user model:

public function rates_sent(){ return $this->hasMany(Rate::class,"sender"); } public function rates(){ return $this->belongsToMany(Rate::class); } 

My current bid model:

 public function sender(){ return $this->belongsTo(User::class,"sender"); } public function receiver(){ return $this->belongsToMany(User::class); } 

The second option:

users (id) | tariffs (id, sender, recipient, comment, bid)

User Model:

 public function rates_sent(){ return $this->hasMany(Rate::class,"sender"); } public function rates_received(){ return $this->hasMany(Rate::class,"receiver"); } 

Speed ​​Model:

 public function sender(){ return $this->belongsTo(User::class,"sender"); } public function receiver(){ return $this->belongsTo(UseR::class,"receiver"); } 

So are both good solutions? Is one option more acceptable than the other? Is there also a better option for this?

I apologize for my bad prints. thanks in advance

+6
source share
1 answer

If you think so: "The user has many bets, but the bid belongs to one user", then you should follow the second option.

Here's what my models and tables look like:

  • users - id

  • bids - id, sender_id, receiver_id, comment, rate

User.php

 public function sentRates() { return $this->hasMany(Rate::class, 'sender_id'); } public function receivedRates() { return $this->hasMany(Rate::class, 'receiver_id'); } 

Rate.php

 public function sender() { return $this->belongsTo(User::class, 'sender_id'); } public function receiver() { return $this->belongsTo(User::class, 'receiver_id'); } 

I think this option is more appropriate than the first.

+1
source

Source: https://habr.com/ru/post/1014725/


All Articles