How to change the default name of an eloquent column

2

I'm in a project that is in production, and by default when we use a foreign key in eloquent we follow the following rule "user_id" for example to store the user id.

Only in the database that I'm working with, they put this field as " id_user " and I'm always going to make the relationship generate an error saying that the " user_id " field was not found. How can I make my code understand that user_id is id_user in my bank?

    
asked by anonymous 20.06.2017 / 14:01

1 answer

2

When you declare the relationship between models you have the second and third parameters of the relationship function to make this change to that convention, for example:

public function address() {
    return $this->hasMany('user', 'foreign_key', 'local_key');
}
    
20.06.2017 / 14:50