i have these tables in my database.
users
id username
then
books
id name user_id
ratings
id rating book_id
Now i want to fetch all the users and list them on the webpage but i need them to be order by ratings. So basically i need to check all users and their books then calculate the average rating of those books then get users according that average rating.
How can i do this using laravel or query builder?
i have relationship setup as following
User model
public function books()
{
return $this->hasMany('App\Book');
}
Book model
public function ratings()
{
return $this->hasMany('App\Rating');
}
Rating model
public function book()
{
return $this->belongsTo('App\Book');
}
P.S. if there is no rating for particular book then i want to return 0 ratings instead of null so in my webpage i could display that users rating as 0 Thanks.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire