Select inside where in laravel

0

The question is that I need to enter a% conditional where , according to the data that is sent to the controller, the question one of those conditions is that I bring the data according to a select , I already tried the query in the manager of data, but in laravel I guess it is represented in another way, this is what I tried.

 $data = DB::table('historico_establecimientos')
                  ->where(function($data) use ($location,$anio,$mes)  {
                     if($location=="due") {
                         $data->where("anio_corte",$anio);
                         $data->where("mes_corte",$mes);
                     }else{
                         $data->where("anio_corte",('select MAX(anio_corte) from historico_establecimientos'));'introducir el código aquí'
                         $data->where("mes_corte",('select MAX(mes_corte) from historico_establecimientos'));
                     }
                  })
                  ->orderBy("nombre_establecimiento","ASC")
                  ->get();

Query sql

select * from historico_establecimientos where anio_corte  = (select 
MAX(anio_corte) from historico_establecimientos) and mes_corte = (select 
MAX(mes_corte) from historico_establecimientos)
    
asked by Andrés Cantillo 22.06.2018 в 21:21
source

1 answer

1

You can use Laravel's Raw Expressions , like this:

$data = DB::table('historico_establecimientos')
            ->where(
                'anio_corte', 
                DB::raw('(select MAX(anio_corte) from historico_establecimientos)')
            ->where(
                'mes_corte',
                DB::raw('(select MAX(mes_corte) from historico_establecimientos)')
            ->orderBy('nombre_establecimiento','ASC')
            ->get();
    
answered by 22.06.2018 / 23:49
source