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

Antonyco50's avatar

Schema / relationship Question

Edited original post to be more concise and clear.

Hi Everyone,

I'm relatively new and have a few questions regarding a personal learning project I'm working on. Thank you in advance for any feedback!

CONTEXT:

My project is using the default laravel 7.x UI and auth scaffolding. (I'v already implemented roles with a roles table and role_user table.

Many users can have many roles:

  1. Customer
  2. Agent
  3. Admin ...

Users with the Role of Agent can have one status at any given time; either 'AVAILABLE' or 'BUSY'.

QUESTIONS:

#1. How should I define the Agent User/Status relationship? Initially I did Many to Many. I created a statuses table and status_user table. Later I concluded that this was incorrect and changed the relationship to Many to One as seen below. ( I didn't change any of my tables)

IN THE USER MODEL:

public function statuses(){

        return $this->belongsTo('App\Status');

    }

IN THE STATUS MODEL:

	    public function users(){

        return $this->belongsToMany('App\User');

    }

#2. After having trouble with some queries I'm starting to think using the two tables (statuses and status_user) is in incorrect given how I modeled them above.

-Should I rollback just add a "status" column directly to the user table? or did I get the entire relationship wrong? -Maybe I shouldn't be relating status to the users table; instead to a separate 'Agents' table?

0 likes
4 replies
estudiante_uap's avatar

So that you can connect the user model with the states model, you have to do the many-to-many relationship in both models

1 like
Antonyco50's avatar

Thanks everyone. I ended up dropping the status_users pivot table, and changed the relationship to Many to One. Everything is working great now!

Please or to participate in this conversation.