Can any body tell me what is the main difference between
the BelongsTo and HasOne relationship in eloquent.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
BelongsTo is a inverse of HasOne.
I'm giving hasOne relation from User to Phone.
Using this relation, I'm able to get Phone model data using User model.
But It is not possible with Inverse process using HasOne. Like Access User model using Phone model.
If i want to access User model using Phone than It is necessary to add BelongsTo in Phone model.
You can refer this link for more detail.
The main difference is which side of the relation holds relationship's foreign key. The model that calls $this->belongsTo() is the owned model in one-to-one and many-to-one relationships and holds the key of the owning model.
Example one-to-one relationship:
Example one-to-many relationship:
Here you can see a good example and see what the difference is between BelongsTo and HasOne relationship in eloquent.
Eloquent Relationships Cheat Sheet by Mahmoud Zalt https://link.medium.com/9lj9BAG8lR
One-to-one relationship: You, as a User, can have one (hasOne) Profile. And of course the inverse also applies. Profile (belongsTo) a User. A user can't have more than one profile and a profile can't belong to multiple users.