laravel雄辩的本地关键是什么

时间:2022-10-25 09:25:15

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

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

what does local key mean in this thing? does it mean 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');

2 个解决方案

#1


4  

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

local_key是表的主键。如果您的主键未被称为id,则只需指定它。

#2


3  

I believe everything is written in the doc:

我相信一切都写在文档中:

ake note that Eloquent assumes the foreign key of the relationship based on the model name. In this case, Phone model is assumed to use a user_id foreign key. If you wish to override this convention, you may pass a second argument to the hasOne method. Furthermore, you may pass a third argument to the method to specify which local column that should be used for the association:

请注意,Eloquent根据模型名称假定关系的外键。在这种情况下,假设Phone模型使用user_id外键。如果要覆盖此约定,可以将第二个参数传递给hasOne方法。此外,您可以将第三个参数传递给方法,以指定应该用于关联的本地列:

Which basically means that 'local_key' is the name of the table column in your db which is responsible to match the related entity (phone) with your current entity (user).

这基本上意味着'local_key'是数据库中表列的名称,它负责将相关实体(电话)与您当前的实体(用户)进行匹配。

If you have a look at the db, I'm sure you'll find a table user with a phone_id column, try to change it to something else (like "phone" only) and your eloquent request will crash. Then change your call to return $this->hasOne('Phone', 'user_id', 'phone'); and this might work again.

如果您查看数据库,我确定您会找到一个带有phone_id列的表用户,尝试将其更改为其他内容(仅限“phone”),您的雄辩请求将崩溃。然后更改您的通话以返回$ this-> hasOne('Phone','user_id','phone');这可能会再次奏效。

#1


4  

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

local_key是表的主键。如果您的主键未被称为id,则只需指定它。

#2


3  

I believe everything is written in the doc:

我相信一切都写在文档中:

ake note that Eloquent assumes the foreign key of the relationship based on the model name. In this case, Phone model is assumed to use a user_id foreign key. If you wish to override this convention, you may pass a second argument to the hasOne method. Furthermore, you may pass a third argument to the method to specify which local column that should be used for the association:

请注意,Eloquent根据模型名称假定关系的外键。在这种情况下,假设Phone模型使用user_id外键。如果要覆盖此约定,可以将第二个参数传递给hasOne方法。此外,您可以将第三个参数传递给方法,以指定应该用于关联的本地列:

Which basically means that 'local_key' is the name of the table column in your db which is responsible to match the related entity (phone) with your current entity (user).

这基本上意味着'local_key'是数据库中表列的名称,它负责将相关实体(电话)与您当前的实体(用户)进行匹配。

If you have a look at the db, I'm sure you'll find a table user with a phone_id column, try to change it to something else (like "phone" only) and your eloquent request will crash. Then change your call to return $this->hasOne('Phone', 'user_id', 'phone'); and this might work again.

如果您查看数据库,我确定您会找到一个带有phone_id列的表用户,尝试将其更改为其他内容(仅限“phone”),您的雄辩请求将崩溃。然后更改您的通话以返回$ this-> hasOne('Phone','user_id','phone');这可能会再次奏效。