Eloquent tends to make handling and working with the help of our dating effortless, and you will supports a number of kind of relationship:

Eloquent tends to make handling and working with the help of our dating effortless, and you will supports a number of kind of relationship:

Databases tables are associated with both. Instance, a blog post may have of a lot statements, or an order would-be regarding the user who put they.

Determining Matchmaking

Eloquent relationships is defined as measures on your Eloquent model classes. Since, for example SwingLifestyle hesap silme Eloquent activities on their own, dating in addition to act as effective ask designers, defining relationship because measures brings strong approach chaining and you can querying possibilities. Like, we would chain extra limitations about this postings dating:

One-to-one

A single-to-one relationships was an incredibly basic relation. Such as for example, a person model would be on the you to Cell phone . To explain this relationships, i lay a telephone strategy with the Affiliate design. The telephone approach will be label the fresh new hasOne method and return the result:

The initial conflict enacted for the hasOne method is title of your own associated design. Once the dating is scheduled, i ic functions. Active features allow you to availableness dating steps since if they had been properties defined for the design:

Eloquent find the newest international trick of one’s relationships based on the design name. In cases like this, the device model try instantly thought getting a person_id international trick. If you wish to override that it seminar, you may want to citation the next dispute towards the hasOne strategy:

Concurrently, Eloquent takes on your international trick need a regard matching the new id (or perhaps the personalized $primaryKey ) column of one’s father or mother. Quite simply, Eloquent will into property value new owner’s id column regarding the associate_id column of Phone listing. If you prefer the connection to use an esteem other than just id , you could violation a third disagreement into hasOne strategy specifying your customized secret:

Identifying This new Inverse Of one’s Relationships

Very, we can supply the device model from our Representative . Today, why don’t we describe a love towards Mobile phone design which can let you access the consumer that possesses the device. We can define brand new inverse out-of good hasOne relationships by using the belongsTo means:

Regarding example above, Eloquent will endeavour to fit an individual_id regarding Cellular telephone design so you can an id on Member model. Eloquent find the brand new standard foreign secret identity of the exploring the title of one’s matchmaking means and suffixing the method name that have _id . However, if for example the foreign trick to your Cellular phone design isn’t member_id , you e since next argument to the belongsTo approach:

In the event the mother design cannot have fun with id as its first secret, or you want to get in on the man model to some other line, you may solution a third conflict towards the belongsTo strategy specifying your mother table’s individualized secret:

You to definitely Of numerous

A single-to-of numerous relationship is utilized to identify dating where one model possesses any number of other patterns. Eg, a blog post have enormous quantities from statements. As with any most other Eloquent relationship, one-to-of numerous dating is actually discussed from the establishing a work on your own Eloquent model:

Think about, Eloquent will automatically determine ideal international trick column into Review model. By the seminar, Eloquent needs the newest “snake case” term of buying design and you can suffix it with _id . So, for it example, Eloquent often guess the latest international secret into Comment model was post_id .

Since the relationship could have been defined, we could accessibility the fresh new distinct comments because of the opening this new comments assets. Think of, because Eloquent provides “vibrant functions”, we could supply relationships procedures since if these people were recognized as features for the model:

Since all of the relationships along with serve as query designers, you can include then limits that comments try retrieved of the contacting the newest statements method and ongoing so you can chain conditions on the query:

Leave a comment

Your email address will not be published. Required fields are marked *