What is the local key in the eloquent laravel

http://laravel.com/docs/4.2/eloquent#relationships

what does the local key mean in this thing? Does this mean the primary key of the table? or what? for example in this code

return $this->hasOne('Phone', 'foreign_key'); return $this->hasOne('Phone', 'foreign_key', 'local_key'); 
+5
source share
2 answers

I believe that everything is written in the document:

ake note that Eloquent assumes a foreign relationship key based on the model name. In this case, it is assumed that the phone model uses the user_id foreign key. If you want to override this convention, you can pass the second argument to the hasOne method. Alternatively, you can pass the third argument to the method to indicate which local column to use for the association:

This basically means that "local_key" is the name of the table column in your db, which is responsible for matching the associated object (phone) with your current object (user).

If you look at db, I’m sure that you will find a table user with a column phone_id, try changing it to something else (for example, β€œphone”), and your eloquent query will work. Then change your call to return $this->hasOne('Phone', 'user_id', 'phone'); and it may work again.

+3
source

local_key is the main key of your table. You only need to specify it if your primary key is not called id .

+3
source

Source: https://habr.com/ru/post/1206426/


All Articles