Support the ongoing development of Laravel.io →
Database Eloquent Queues

Can someone explain me why I have to use the local_key in this relationship?

My table User have a primary key user_id and a unique column email My table Order have a foreign key email link the User.email column not the User primary key.

if I use

public function order() {
    return $this->hasMany('App\Order', 'email');
}

this don't work

But using

public function order() {
    return $this->hasMany('App\Order', 'email', 'email');
}

I recieve all the info, but couldn't understand why.

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

alexisdeza alexisdeza Joined 19 Nov 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.