getKeyName()
will get the primary key for the model.
(getKeyName()
将获取模型的主键。)
it supports to return id
, after you change it to slug
, it will return slug
(它支持返回id
,将其更改为slug
,它将返回slug
)
And hasMany
Here's the source code ;
(和hasMany
这里的源代码 ;)
The third parameter LocalKey
will use getKeyName()
when it's empty. (第三个参数LocalKey
空时将使用getKeyName()
。)
If you still want to use hasMany, you need to pass the third parameter like this:
(如果仍然要使用hasMany,则需要像这样传递第三个参数:)
public function images()
{
return $this->hasMany(Image::class, 'product_id', 'id');
}
This will convert the Eloquent query to database query, which will take the right local key products.id
.
(这会将Eloquent查询转换为数据库查询,该查询将使用正确的本地密钥products.id
。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…