Laravel - Relationships - Has One Through
The "has-one-through" relationship defines a one-to-one relationship with another model. However, this relationship indicates that the declaring model can be matched with one instance of another model by proceeding through a third model.
For example, in a vehicle repair shop application, each
Mechanic model may be associated with one
Car model, and each
Car model may be associated with one
Owner model. While the mechanic and the owner have no direct relationship within the database, the mechanic can access the owner through the
Car model. Let's look at the tables necessary to define this relationship:
mechanics id - integer name - string cars id - integer model - string mechanic_id - integer owners id - integer name - string car_id - integer
Now that we have examined the table structure for the relationship, let's define the relationship on the
hasOneThrough(Owner::class, Car::class); } }
The first argument passed to the
hasOneThrough method is the name of the final model we wish to access, while the second argument is the name of the intermediate model.