Okay, I'm working through the Laravel 4 docs to setup a one-to-many relationship between two models. Obviously, one side should use hasMany(). But for the other side, should I use hasOne or belongsTo? Does it matter? What's difference? Why do both exist?
I had thought hasOne would be for one-to-one relationships, and belongsTo would be for the one side of one-to-many. But in the docs, for inserting a related model here:
http://laravel.com/docs/eloquent#inserting-related-models
they are using save()
which seems to only be present in hasOne
and hasMany
relationships, not in belongsTo
. It looks like belongsTo
uses associate()
for the same purpose:
Maybe I just need some general background on when to use belongsTo
vs. hasOne
, and why belongsTo
uses associate()
while hasOne
uses save()
.
EDIT: I guess my point of confusion was that in the docs (http://laravel.com/docs/eloquent#inserting-related-models), they used:
$post->comments()->save($comment);
where I would have used:
$comment->post()->associate($post);
Is there an advantage to one way or the other? Or is it just a question of what makes sense in the context?