[英]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();
如何將值限制為“ 1”? 我想獲取表“ 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');
當您使用max('column_name')
,它將返回一個計數值。 在這種情況下, $maxValue
將保存您的列數。 這是laravel中可用的聚合函數。
您不能調用max()->limit()
,就像在整數值中調用->limit()
,它會引發錯誤:
在整數上調用成員函數limit()
但是,如果要獲得特定條件的最大值,則需要使用group by子句。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.