How to enter a subquery in laravel 5.2

4

I have this query in mysql

SELECT id, name, (SELECT COUNT(id_team_local) FROM seasons WHERE id_team_local = teams.id) AS 'Partidos Jugados' FROM teams

and I would like to give Laravel some idea of how I could do it?

    
asked by Miguel Juarez 22.09.2016 в 19:45
source

1 answer

1

Taking into account that you do not specify if you are using Eloquent or if there are related models, I will give an example by directly accessing the database through the facade (DB) and the query builder:

$teams = DB::table('teams')
    ->select(DB::raw('id, name, (SELECT COUNT(id_team_local) FROM seasons WHERE id_team_local = teams.id) AS 'Partidos jugados'))
    ->get();

More information in the Laravel documentation: link

    
answered by 22.09.2016 / 20:02
source