Home > Mobile >  Need help writing this sql query into laravel query builder
Need help writing this sql query into laravel query builder

Time:03-27

SELECT *
FROM Movies 
WHERE (SELECT AVG(Score) 
FROM Reviews WHERE MovieId=Id)>2

Is it even possible to write subqueries in laravel query builder?

CodePudding user response:

Try this:

DB::table('movies')
      ->join('reviews', 'reviews.movieId', '=', 'movies.id)
      ->select(DB::raw('avg(reviews.score) as score'))
      ->where([
            ['reviews.movieId', '>', 2],
            // [ if any other clause]
            ])
      ->get();
  • Related