I'm already working in a web application in Laravel that needs a rating system between users. So, I've been reading the Laravel documentation and some posts here and there but I keep wondering which is the best way to accomplish what I want to do. I can't decide between the next two options.
My current tables are:
users (id) | rates (id,sender,comment,rate) | 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 Rate model:
public function sender(){
return $this->belongsTo(User::class,"sender");
}
public function receiver(){
return $this->belongsToMany(User::class);
}
The second option is:
users (id) | rates (id,sender,receiver,comment,rate)
User model:
public function rates_sent(){
return $this->hasMany(Rate::class,"sender");
}
public function rates_received(){
return $this->hasMany(Rate::class,"receiver");
}
Rate 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 another and better option to do this?
Apologies for my bad engrish. Thanks in advance
If you think this way: "User has many rates, but rate belongs to one user" then you should follow second option.
This is how my models and tables would look like:
users - id
rates - 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 then first one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With