Eloquent: Relationships. Eloquent relationships are understood to be practices on the Eloquent model classes.
Introduction
Database tables in many cases are linked to each other. For instance, a blog post might have comments that are many an order might be pertaining to an individual whom put it. Eloquent makes handling and dealing with your relationships simple, and supports a number of common relationships:
Defining Relationships
Since relationships additionally act as effective question builders, determining relationships as practices provides effective technique chaining and querying capabilities. For instance, we might chain query that is additional on this articles relationship:
But, before diving too deep into making use of relationships, why don’t we learn to determine every type of relationship supported by Eloquent.
Anyone To One
A relationship that is one-to-one a very fundamental variety of database relationship. As an example, a person model may be related to one Phone model. To determine this relationship, we shall spot a phone technique regarding the consumer model. The phone technique should phone the hasOne method and get back its outcome. The hasOne technique can be obtained to your model through the model’s Illuminate\Database\Eloquent\Model base course:
The very first argument passed away to your hasOne technique could be the title of this associated model course. After the relationship is defined, we possibly may retrieve the record that is related Eloquent’s powerful properties. Dynamic properties permit you to access relationship techniques just as if these were properties defined in the model:
Eloquent determines the international key of this relationship on the basis of the moms and dad model title. In this situation, the telephone model is immediately thought to own a user_id international key. Continue reading “Eloquent: Relationships. Eloquent relationships are understood to be practices on the Eloquent model classes.”