简体   繁体   中英

Hi i want to get MAX value in leftJoin or LIMIT 1 in Laravel, how can i do it?

$almacenes = Almacenes::with(['producto'])->
                 leftJoin('albaranes_lineas',function ($k) use ($request){
                     $k->on('almacen.id_producto','=','albaranes_lineas.id_producto');


                 })->where(function ($q) use ($request) {

                $q->where('almacen.id_usuario', $request->usuario);
                $q->where('almacen.id_local', $request->local);
                $q->where('almacen.tipo', $request->tipo);
                $q->whereRaw('HOUR(almacen.created_at) = ?', $request->hora);
                $q->whereDate('almacen.created_at', '=', $request->fecha);
                //$q->where('inventario', 1);


            })->get();

how can i limit the value to "1"? I want to get the last field of each row in the table "albaranes_lineas".

$maxValue = Almacenes::with(['producto'])->
                 leftJoin('albaranes_lineas',function ($k) use ($request){
                     $k->on('almacen.id_producto','=','albaranes_lineas.id_producto');
                     $k->where('albaranes_lineas.id',821);
                 })->max('table_name.column_name');

When you use max('column_name') , it returns a single count value. In this case $maxValue will hold the count of your column. This is an aggregate function available in laravel.

You can not call max()->limit() as it will be like calling ->limit() in an integer value and will throw error :

Call to a member function limit() on integer

However, if you want to get max for a particular condition, then you you will need group by clause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM