mardi 23 janvier 2018

Laravel: joining 3 tables and compute average of a column

I am currently learning Laravel and I am using DB class when joining the three tables. I am able to join the three tables but I need to get the average of a certain column for a teacher (ratings table, rating column), here's what I have and I am stuck here.

here's my table design

enter image description here

and here's my query

$teachers = DB::table('ratings as r')
                    ->join('users as u','r.teacher_id', '=', 'u.id')
                    ->join('user_infos as ui','r.teacher_id', '=', 'ui.user_id')
                    ->select('r.rating','ui.about','ui.first_name','ui.last_name','ui.avatar','ui.rate','u.*')
                    ->where('u.status', 1)->get();

also, the results for same user is being repeated. the user has two ratings from the ratings table and it appear two times in my view.

enter image description here

Please help guys. Thank you!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire