jeudi 29 mars 2018

Laravel - Pull only Most Recent Record Value into Index Blade @foreach

In my index blade of equipment, I have the following lines:

            @foreach ($equipment as $equipment)

                 <tr>
                     <td><a href="/origins/"></a></td>
                      <td></td>
                      <td></td>
                     <td></td>
                     <td></td>
                     <td></td>
                </tr>

            @endforeach

As you can see, the last column pulls in "registrations". As they sit now, they arrive as an array such as below:

[{"id":45,"equipmentID":368,"amountDue":"0.00","emissions":0,"createdBy":1,"created_at":null,"updated_at":null,"expirationDate":"2015-05-31"},{"id":63,"equipmentID":368,"amountDue":"0.00","emissions":0,"createdBy":1,"created_at":null,"updated_at":null,"expirationDate":"2014-03-31"},{"id":274,"equipmentID":368,"amountDue":"1594.57","emissions":0,"createdBy":1,"created_at":null,"updated_at":null,"expirationDate":"2012-11-30"}]

How would I go about sorting it so the record with the most recent "expirationDate" comes first and the date itself is the thing printed to the index (in the above case, I'd be looking for the date 2015-05-31).

Would this be an attribute through the model?

Before I forget, my current relationship between the two models (the equipment and the registrations) is defined as such:

public function registrations()
{
    return $this->hasMany(EquipmentLicense::class,'equipmentID','id');
}

Thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire