Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

plue's avatar
Level 1

get first row in date

how can i get the first data here in groupby date

 public function index() {
        $station = Station::query()
        ->select(['date', 'tank', 'qty_out', 'qty_in'])
        ->selectRaw('SUM(CASE WHEN tank = ? THEN qty_out ELSE 0 END) AS tank1_out', ['TANK 1'])
        ->selectRaw('SUM(CASE WHEN tank = ? THEN qty_out ELSE 0 END) AS tank2_out', ['TANK 2'])
        ->selectRaw('SUM(CASE WHEN tank = ? THEN qty_out ELSE 0 END) AS tank3_out', ['TANK 3'])
        ->selectRaw('SUM(CASE WHEN tank = ? THEN qty_out ELSE 0 END) AS tank4_out', ['TANK 4'])
        ->selectRaw('SUM(CASE WHEN tank = ? THEN qty_in ELSE 0 END) AS tank1_in', ['TANK 1'])
        ->selectRaw('SUM(CASE WHEN tank = ? THEN qty_in ELSE 0 END) AS tank2_in', ['TANK 2'])
        ->selectRaw('SUM(CASE WHEN tank = ? THEN qty_in ELSE 0 END) AS tank3_in', ['TANK 3'])
        ->selectRaw('SUM(CASE WHEN tank = ? THEN qty_in ELSE 0 END) AS tank4_in', ['TANK 4'])
        ->groupBy('date')
        ->orderBy('date', 'DESC')
        ->get();

        $balance = Station::select('id', 'tank1_prev')->first(); //im trying to get the first row in every date

        return view('dashboard.dashboard', compact('station', 'balance'));
    }

blade

@foreach ($station as $data)
      <div class="accordion" id="accordionExample">
      <div class="accordion-item">
     <h2 class="accordion-header" id="headingThree">
     <button class="accordion-button collapsed" type="button" data-toggle="collapse" data-target="#collapseThree{{$loop->iteration}}" aria-expanded="false" aria-controls="collapseThree">
                        {{{ \Carbon\Carbon::parse($data->date)->format('F d Y') }}}
      </button>
       </h2>
     <div id="collapseThree{{$loop->iteration}}" class="accordion-collapse collapse" aria-labelledby="headingThree" data-parent="#accordionExample">
  <div class="accordion-body">
 <strong>Tank 1 Total Out => {{ $data->tank1_out }}</strong> <br>
 <strong>Tank 2 Total Out => {{ $data->tank2_out }}</strong> <br>
 <strong>Tank 3 Total Out => {{ $data->tank3_out }}</strong> <br>
 <strong>Tank 4 Total Out => {{ $data->tank4_out }}</strong> <br>
  <strong>Tank 1 Balance => {{ $balance->tank1_prev }}</strong> <br> <!--  here i want to show the data in every first row of date but this returns the id 1 in every date -->
                      </div>
                    </div>
                  </div>
                  @endforeach
0 likes
4 replies
tisuchi's avatar

@plue How about this?

$balance = Station::whereDate('date', $data->date)->latest()->first();
plue's avatar
Level 1

@tisuchi i tried this before and gave me

Property [date] does not exist on this collection instance.
PovilasKorop's avatar
Level 11

I don't think it's possible to get the SUM by date and the FIRST record by date in one SQL/Eloquent query.

You should probably have an Eloquent query to get ALL the data, and then in Collections calculate both first and sum.

1 like

Please or to participate in this conversation.