Home > Mobile >  Get all the row using where min/max laravel
Get all the row using where min/max laravel

Time:11-22

I'm trying to get all the row where I have the minimum value in 'temperature' but using min() or max() returning only the minimum or maximum values

At the moment I've to find before the minimum and than the 'acktime' I would like to get all the row in one time, but:

$tmp = rawData::whereDate('acktime','=', $thisDay)->min('temperature');

In this way I get only the minimum value

$tmp = rawData::whereDate('acktime','=', $thisDay)->get()->min('temperature');

In this way I get only the minimum value

 $tmp = rawData::whereDate('acktime','=', $thisDay)->get('acktime')->min('temperature');

In this way I get null

$tmp = rawData::with('acktime','temperature')->whereDate('acktime','=', $thisDay)->min('temperature')->get();

In this way I get error Call to a member function get() on float

CodePudding user response:

You could try this:

$tmp = rawData::whereDate('acktime','=', $thisDay)->orderBy('temperature', 'asc')->first();

CodePudding user response:

You can build your own select statement. Using DB::raw to do min operation. This will return one row, with the expected min data and acktime.

$data = rawData::with('acktime','temperature')
    ->whereDate('acktime','=', $thisDay)
    ->select('acktime', DB::raw('min(temperature) as temperature'))
    ->first();

CodePudding user response:

In addition to @mrhn. You can try this raw statment to get Min and Max tempr. :

$data = rawData::with('acktime','temperature')
    ->whereDate('acktime','=', $thisDay)
    ->select('acktime', \DB::raw('MIN(temperature) as temperatureMin, MAX(temperature) AS temperatureMax'))
    ->get();
  • Related