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

LaraBABA's avatar

Best way to assign 2 user_id to a single table

Hello,

Quick question please.

I have 2 tables:

table "users"

table "ads"

In the table "users" you have users and admins

In the table "ads" I need two columns with foreign keys:

user_id admin_id

When I will create an "ad", I will assign a user to it but also 1 admin id as the account manager

Where I am confused is during the relations, how will it work?

In the "ad" model I have:

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

But how to add it twice as:

 public function user()
   {
       return $this->belongsTo(User::class, 'admin_id');
   }

how to get both keys in the "ad" model please?

Thank you.

0 likes
2 replies
MichalOravec's avatar
Level 75

In your ad model

public function user()
{
   return $this->belongsTo(User::class);
}

public function admin()
{
   return $this->belongsTo(User::class, 'admin_id');
}
$ad = Ad::first();

$ad->user;

$ad->admin;
1 like
LaraBABA's avatar

Brilliant, thanks, I always thought that Laravel needed to see a function name related to the model name you use, I get it now......Thank you so much!

Please or to participate in this conversation.