02 - Filter Model By user_id: With where() and Global Scope
So now that we have the user_id in our DB table, let's filter by that, so that each user would access only their projects. Option 1
We found 22 results for "eloquent relations".
So now that we have the user_id in our DB table, let's filter by that, so that each user would access only their projects. Option 1
Polymorphic relations are challenging to understand at first glance. So, the best way to explain it is with examples
Today I want to talk about a feature of Laravel which is really useful but can be potentially difficult to understand at first. Pivot table is an example of intermediate table with relationships between two other "main" tables.
This is a deep dive into belongsToMany Relationships in Laravel with examples - from simple, to much more complex.
If you work with a project that has multiple levels of hasMany relationships, you can use hasManyThrough instead, or one of a few packages I will show in this video.
Polymorphic relations are one of the most difficult concept to understand for junior developers, and I tried to do it with a real-life example.
I see various bugs and problems people face with foreign key migrations, so I decided to talk about different syntax options and things to know.
Eloquent has a lot of "hidden gems". In this tutorial, let's see how we can get the latest record from the `hasMany` Relationship in five different ways.
I find more and more Eloquent functions that are rarely used, so continue posting these short tips on the blog. Here's the next one.
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?
I get a lot of questions about database structure, and in this video I will practically test two different DB structures and we will try to see which is better: belongsTo with three columns, or polymorphic relations?
This video is a 4-in-1 lesson from my course "Eloquent: Expert Level" about N+1 Query Problem.
There are many ways how to deal with deleting the records that are in a relationship. In this video, I will show the default behavior, and how we can change it in Laravel validation, in migrations, or with soft-deletes.
Filter, sort and include eloquent relations based on a request.
This extended version of HasManyThrough allows relationships with unlimited intermediate models.
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
A free lesson from my course "Structuring Databases in Laravel 11".
Another *practical* video of helping someone on Laracasts forum to query the data. This video has a "plot twist" where I decided to make my code even better.
Quite a regular task, but not a simple solution: how to order query results by a belongsTo relation column? I will show you one "bad" way, and two good ways to do it.
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.
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.