lundi 26 octobre 2015

Laravel Eloquent sum of column in another table

so I have this output:

enter image description here

Here is the code in my controller:

public function showToday()
    {
        $now = new DateTime('today');
        $today = $now->format('Y-m-d');
        $reservations = Reservation::with('room', 'client') ->where('reservation_from', $now)
                                                            ->orderBy('created_at')
                                                            ->get();

        return \View::make('pages.admin.report.today') -> with('reservations', $reservations)
                                                       -> with('now', $today);
    }

Schema:

enter image description here

View (blade)

  <div class="box-body table-responsive no-padding">
    <table class="table table-bordered">
      <thead>
        <tr>
          <th>Room #</th>
          <th>Client Name</th>
          <th>Reservation From</th>
          <th>Reservation To</th>
          <th>Booked At</th>
          <th>Amount</th>
      </thead>
      </tr>
      <tbody>
        @foreach($reservations as $res)
        <tr>
          <td>{{ $res -> room -> room_number }}</td>
          <td>{{ $res -> client -> name }}</td>
          <td>{{ date('F d, Y', strtotime($res -> reservation_from)) }}</td>
          <td>{{ date('F d, Y', strtotime($res -> reservation_to)) }}</td>
          <td>{{ $res -> created_at }}</td>
          <td>{{ $res -> room -> price }}</td>
        </tr>
        @endforeach
        <tr>
          <td colspan="2"><strong>Total:</strong></td>
          <td></td>
          <td></td>
          <td></td>
          <td><!-- Display price total here --></td>
        </tr>
      </tbody>
    </table>
  </div>

What I wanted to is to display the total amount by adding the prices from the rooms table. How can I do that with Laravel and Eloquent? Thank you in advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire