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

mac03733's avatar

Relationship with unknown foreign key ( foreign key can be one of two)

hi guys ...i have 3 tables ... 1)users 2)admins 3)messages

use case

->user can message user ->admins and users can message ->admins can message admin

now my question is can anyone advice on how i would set my relationships "considering the foreign key for--> sender and recipient column can change depending on whether its an admin or user model "

so i think i cant do somthing like this

  public function sender(){
      return $this->belongsTo('App\User');
    }

because sender could also be

  public function sender(){
      return $this->belongsTo('App\Admin');
    }

same goes for recipient

0 likes
9 replies
martinbean's avatar

@mac03733 If the relation can be one of many models, then you have a polymorphic relationship.

This is also another reason to have just a single user model to represent all users in your application, and use authorisation to determine what they can see, rather than creating other, ad hoc user models like “admin”.

mac03733's avatar

@martinbean using authorization is not bad.. but if the actors are gonna have different attributes i belive its better to separate them

cmdobueno's avatar

People also once believed the earth was flat and cocaine was good for the common cold... it is not better to separate them.

All you have to do is go back to the basics of oop.

an actor is a user. Sure they have additional data, but thats what you would link to.

Example schema (not a huge amount of thought here... just real quick)

users: id, name (divide it into two fields if you desire... i do), password, email, role_id and all that jazz

actor_details: user_id (Foreign Key to Users and index/primary key) other fields you want here...

then add in roles roles: id, name, OTHERFIELDS?

There that should work for the basics, then your user data is all saved in a single table, and you have additional tables to control the "Power" and "Additional" details

cmdobueno's avatar

Or even better just throw a flag on users table to admin...

is_admin tiny int

1 like
mac03733's avatar

@cmdobueno and @martinbean somthing to concider on my next project ...THANKS.

being new to laravel and all ..one tutorial says this is best next one says that is best :(

would appreciate some help with my current problem

cmdobueno's avatar

You can not do that in a relationship the way you think,

Your best bet is to use an attribute...

public function getSenderAttribute(){

    if( LOGIC TO SEE IF ADMIN){
        return Admin::find(YOUR_SENDER_ID);
    }else{
        return User::find(YOUR_SENDER_ID);
    }

}

I do not know based on the info what is required if a sender is an admin or not, so this is the best I can do, as well as I did not want to guess your sender_id field...

mac03733's avatar

will continue trying to find solve it..thanks for feedback

cmdobueno's avatar

The reason you can not use a relationship, is that those queries for loading are run without the queires results.

So the relationship has no idea what the column values are... so you can not use conditional login on the records values.

This is why I suggested the attribute I provided. It will do what you want, it is just not a relationship. You can still add it in the model so it appends if you are converting to an array (like if you send this model to VueJs)

I am very confident you are not going to find anything else... at least not a way to use a direct relationship. You seem to be stuck using this database layout so... this is the best you can get right now is an attribute...

martinbean's avatar

People also once believed the earth was flat and cocaine was good for the common cold

@cmdobueno I can’t wait until the winter, ha-ha!

1 like

Please or to participate in this conversation.