Support the ongoing development of Laravel.io →
posted 9 years ago
Eloquent
Last updated 2 years ago.
0

In this case I believe you should use hasOne instead.

BelongsTo is mostly used together with hasMany, example a folder that has many files, and each file belongs to a folder.

0

In this case you would use hasOne/hasMany, since the foreign key is on the other table of this relationship. belongsTo will be on the other model (Category and Brand), unless you swap the FK.

However you should check http://laravel.com/docs/4.2/eloquent#polymorphic-relations, it might be the way to go if you find it nice and easy.

0

If i use hasOne, eloquent told me that i does not find the category_id field in Picture table.

My DB is like this :

DB

It works with belongsTo but not with hasOne.

0

Yes, this is what you need. It's in my post, which was an answer to the previous by @mengidd - didn't mention that ;)

But check the polymorphic relations.

0

Ok, i see what you mean :) thanks, and yes, i'll check polymorphic !

Thanks again

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.