Laravel Fluent Query Builder Join with subquery

前端 未结 7 674
天涯浪人
天涯浪人 2020-11-27 13:03

Okay after hours of research and still using DB::select I have to ask this question. Because I am about to trough my computer away ;).

I want to get the last input o

7条回答
  •  迷失自我
    2020-11-27 13:20

    You can use following addon to handle all subquery related function from laravel 5.5+

    https://github.com/maksimru/eloquent-subquery-magic

    User::selectRaw('user_id,comments_by_user.total_count')->leftJoinSubquery(
      //subquery
      Comment::selectRaw('user_id,count(*) total_count')
          ->groupBy('user_id'),
      //alias
      'comments_by_user', 
      //closure for "on" statement
      function ($join) {
          $join->on('users.id', '=', 'comments_by_user.user_id');
      }
    )->get();
    

提交回复
热议问题