📜  avg rating get in join in laravel 8 - PHP 代码示例

📅  最后修改于: 2022-03-11 14:53:43.747000             🧑  作者: Mango

代码示例1
$users = User::where('type', 'instructor')->get();
  foreach ($users as $user) {
      $user['ratings'] = $user->ratings()->avg('rate');
  }
  return $users;


$data = User::with('ratings')
->join('Rating table','user.id','=','Rating table.user_id')
->select('user.*',DB::raw('avg(rating_for_user)'))
->get();