Reputation: 6707
All I'm trying to do is understanding when exactly should I use hasOne()
and when should I use belongsTo()
. Both seem identical to me. For example, here is my model:
use Illuminate\Database\Eloquent\Model;
use App\Categories;
use App\User;
class tickets extends Model
{
protected $table = "tickets";
public function category()
{
return $this->hasOne(Categories::class, 'id', 'category_id');
}
public function user()
{
return $this->hasOne(User::class, 'id', 'user_id');
}
}
I can do the same by using belongsTo()
function too. Just I should put them into user
and category
models instead. Anyway, when should I use either hasOne()
or belongsTo()
?
Upvotes: 3
Views: 11440
Reputation: 5973
hasOne
is a 1:1, or one-to-one relationship.
hasMany
is a 1:n, or one-to-many relationship.
The belongsTo
method in Eloquent is used to define the inverse of these relationships.
The definition of these will depend on your data model.
In your case:
You have a Category
model, which hasMany
Ticket
s.
You also have a User
model, which hasMany
Ticket
s.
Now from the Ticket perspective, you would want to define the inverses of these 2 hasMany
relationships. You will do this by defining a belongsTo
.
So the Ticket belongsTo
a User
and belongsTo
a Category
.
To answer your question:
From the Ticket
s perspective, it is a 1:1 relation, because the foreign key in the Ticket model points to 1 User and the category foreign key points to 1 Category.
But since the relation you created is a 1:n (one-to-many) and you have also defined it on the User and Category models, you should define the relation in your Ticket model as the inverse of those relations, and the inverse of a hasMany (and hasOne) is belongsTo
.
When defining your relations in Laravel, keep your database schema in mind and define your relations in the same way that they exist in your database schema.
Upvotes: 2
Reputation: 1309
These are same with a single difference. Both returns the single associated object with one difference. When we declare some relation as belongsTo it means there is a database table which has a foreign key of some other table. When we declare hasOne relation it means that this table's primary key has been referenced in another table. Think of it as a parent child table. When we would make the child table we reference each child to its parent, right? This is belongsTo. And when we would make the parent table we know that each entry in parents table can have a single or many entries in the child table. That's hasOne or hasMany relation. You can ask further if you need any more clarification.
Upvotes: 1
Reputation: 7111
Anyway, when should I use either hasOne() or belongsTo() ?
Think of it like what would be hasMany
if there is one to many relation probable in the future. For example: consider User and Phone models. Nature of relation would be User hasOne
Phone, but if you would like to extend functionality for it so user could have multiple phone numbers registered User would still have has*
relation while Phone would keep belongsTo
relation. Just consider which one could be "parent" entity and that one should have hasOne
relation in method. I would always consider User as parent entity and for me logically would be user has one ticket.
Also, try to stick with Eloquent/Laravel/artisan naming convention and name that model Ticket
and other one Category
(Eloquent and Laravel will solve plural where needed i.e. table name).
Upvotes: 2
Reputation: 1944
When dealing with 1 to many relationships, you will have a hasMany()
and a belongsTo()
.
The rule of thumb I use, is if a table has a foreign key (tickets table has a user_id
fk) then this model belongsTo
users.
The same with Category.
So your above example, Ticket
belongsTo
User
& Category
.
Inversely, User
hasMany
Ticket
and similarly Category
hasMany
Ticket
Upvotes: 3