M
M
MVP_Master2021-01-12 17:52:38
Laravel
MVP_Master, 2021-01-12 17:52:38

How in Laravel to link a table with the last record of another by key?

goods are stored in one table. The rest is in another. each product corresponds to many rows of the remainder of another table. We need to get the goods and only the last record of the balance. I can't find the syntax.

public function rests() {
        return $this->hasMany('App\Models\ProductRest', 'product_id', 'id');
}


Here, you need to add something like latest() to this code in order to get only one record using the with construction. But something doesn't work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jazzus, 2021-01-12
@jazzus

public function latestRest()
{
    return $this->hasOne(ProductRest::class)->latest();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question