Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

m615's avatar
Level 2

What is the proper way to have a belongsToMany / hasOne field

I have a User model and I've created an organization model. I User should have the ability to create a new Organization and edit the organization

I've added an organization_id to my Users table so that once the Organization is created I can find it through the $this->hasOne method in my Model.

The organization model in turn has a method that returned all the users that are part of the organization by calling $this->belongsToMany()

In my Nova resource for the User I would like to use the HasOne field in order to show the organization the user is part of and if they are not part of one they can Create an organization.

When I add the HasOne field to my User nova resource and go to the details view I get the following error.

Undefined column: 7 ERROR: column organizations.user_id does not exist

I do not think I need a user_id column in the organizations table because of I want more than one user to belong to the organization?

0 likes
3 replies
bestmomo's avatar
bestmomo
Best Answer
Level 52

Looks like you need a hasMany and belongsTo relations.

1 like
m615's avatar
Level 2

That did the trick. Thank you.

Please or to participate in this conversation.