Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clarify how to setup a one-to-many relationship in Laravel's Eloquent ORM

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:

https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php#L188

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?

like image 500
eimajenthat Avatar asked Jun 01 '13 04:06

eimajenthat


People also ask

How do you create a one to many relationship?

To create a one-to-many relationship The field on the one side (typically the primary key) of the relationship must have a unique index. This means that the Indexed property for this field should be set to Yes (No Duplicates). The field on the many side should not have a unique index.

What is one to many relationship in database with example?

In a one-to-many relationship, one record in a table can be associated with one or more records in another table. For example, each customer can have many sales orders. In this example the primary key field in the Customers table, Customer ID, is designed to contain unique values.


1 Answers

Please refer to the laravel docs on the one to many relationship between posts and comments http://laravel.com/docs/eloquent#relationships. (Where one post has many comments and a comment belongs to a post).

The tables for posts and comments be as follows

Posts Table id | title | body

Comments Table id | comment | post_id

The database table that contains a foreign key belongs to a record in the other table, therefore, in the comments model you must specify that comments belongs to posts.

You are correct that the hasOne relationship only applies to one to one relationships.

Here is a blog post with laravel 3 code which gives an explanation into how eloquent relationship methods work.

http://laravel.io/topic/14/how-eloquent-relationship-methods-work

like image 94
Thomas Clarkson Avatar answered Oct 10 '22 21:10

Thomas Clarkson