Ad

Can Laravel HasOne Be Used With BelongsToMany?

- 1 answer

Let's say I have two tables:

Users: id, name, country_id

Countries: id, name

Of course each user can only have one country, but each country is assigned to multiple users.

So would it be safe to have a User model that utilizes hasOne and a Country model that uses belongsToMany method?

Documentation makes it seem like you can't mix and match different types of relationships.

Ad

Answer

What you are describing is actually a One To Many relationship, where one country has many users. Your Country model should utilize a hasMany relationship, while your user would have a belongsTo relationship.

Ad
source: stackoverflow.com
Ad