Skip to main content

Search Results for "eloquent relations"

We found 22 results for "eloquent relations".

Tutorial 2 min read Free

Eloquent Relations: how to order topics by newest posts?

Quite a typical scenario, but not easy to find the answer. You can easily order Eloquent results by some column, but what if you want to order by related column in child table? Most common example is a forum like Laracasts which shows topics in order by the latest post in that topic. How to do that?

Package 4336 stars Last updated November 2025

spatie/laravel-query-builder

Filter, sort and include eloquent relations based on a request.

Package 2827 stars Last updated November 2025

staudenmeir/eloquent-has-many-deep

This extended version of HasManyThrough allows relationships with unlimited intermediate models.

Package 1188 stars Last updated November 2025

topclaudy/compoships

Compoships offers the ability to specify relationships based on two (or more) columns in Laravel's Eloquent ORM. The need to match multiple columns in the definition of an Eloquent relationship often arises when working with third party or pre existing schema/database

Tutorial 1 min read Free

belongsTo() and withTrashed() - linking to deleted row

Let's say we have DB table products, which is linked to table categories with a field products.category_id = categories.id, Eloquent helps us to define a relation easily. But what if the category gets (soft) deleted, but we still need to have that relationship to be returned for history reasons? You can use withTrashed() method here.

Tutorial 1 min read Free

Eloquent Relationships - with "automatic" orderBy

Quick tip for today - let's imagine we have a one-to-many relationship between Categories and Products, and we often show list of products here and there in our app. What if we want them ordered by name alphabetically? Should we add orderBy in every query? No, we can define that in relationship itself.