vendredi 24 mai 2019

Laravel 5.4 get latest related model

Using Laravel 5.4

I have 2 tables:

coffees (columns: id, name, etc.)

roasts (columns: id, coffee_id, roast_date)

The roasts table can have many reocords for each coffee or none at all.

My relationships:

In Coffee Model:

public function roast()
{
    return $this->hasMany('App\Roast');
}

In Roast Model:

public function coffee()
{
    return $this->belongsTo('App\Coffee');
}

I want to get a list of all the coffees with the latest roast date for each one.

Is there a way to do it with Eloquent?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire