I'll explain with a different example.
###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.
###One-to-many relationship: You as a User, can have many (hasMany) Task(s). The inverse would be Task(s) (belongsTo) a User. So that means that a user can have/create many tasks, but each task only belongs to one user.
###Many-to-many relationship: You as a User can belong to many (belongsToMany) (i.e. forum) Group(s). The inverse would be that a Group can also belong to many (belongsToMany) User(s). That means that a user can join as many groups as he wants, and of course a single group can have unlimited users inside.
There are more examples, but the best would be if you check the documentation. Try using different approaches and see what results you will get.
pogachar said:
I'll explain with a different example.
###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.
###One-to-many relationship: You as a User, can have many (hasMany) Task(s). The inverse would be Task(s) (belongsTo) a User. So that means that a user can have/create many tasks, but each task only belongs to one user.
###Many-to-many relationship: You as a User can belong to many (belongsToMany) (i.e. forum) Group(s). The inverse would be that a Group can also belong to many (belongsToMany) User(s). That means that a user can join as many groups as he wants, and of course a single group can have unlimited users inside.
There are more examples, but the best would be if you check the documentation. Try using different approaches and see what results you will get.
Thanks for the explain :-) From the basic i know when to use which relation but I was wondering when laravel needs a hasOne or belongsTo. But anyway, thank you for your answer!
DrPrez said:
One other thing that is pretty good to know is that when you use belongsTo and belongsToMany you're telling Laravel that this table holds the foreign key that connects it to the other table.
And of course hasOne and hasMany is telling Laravel that this table does not have the foreign key.
Perfect! Exactly the explain I was looking for!! :-) Thanks a lot.
This is a great explanation, thanks! One question: does the hasOne/belongsTo relation also apply to relationships when a user can have either zero OR one of the related model?
If the SQL field is nullable (I mean it is not "NOT NULL") the user can have 0 matching records.
Thank you very much for this explanation. The comparison between the two and the reason why - the inclusion of a foreign key - gives me an explanation for something that was beginning to seem arbitrary.
Clarity is vital for understanding!
When you use belongsTo and belongsToMany you're telling Laravel that this table holds the foreign key that connects it to the other table.
[When you use] hasOne and hasMany [you are] telling Laravel that this table does not have the foreign key.
For example, if you had a users table and posts table, and a user could have many posts. The post_id would not be included in the users table. The user_id would be a foreign key in the posts table. Therefore, we would have the following:
User model:
public function posts(){
return $this->hasMany('App\Post');
}
Post model:
public function user(){
return $this->belongsTo('App\User');
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community