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;
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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.
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;
Please or to participate in this conversation.