【发布时间】:2019-07-27 05:15:14
【问题描述】:
我有三个数据库表:
attributes
---------------------------------
| id | name | showed_name | class
attributes_profiles
----------------------------------------------
| id | attribute_name | profile_id | content |
profiles
------------
| id | ... |
当用户访问个人资料站点时,我想从表中加载所有属性,但我还想从中间表 (pivot) 中为该用户 (id) 加载内容。
public function Attribute(){
return $this->belongstoMany('App\Attribute', 'attributes_profiles', 'profile_id', 'attribute_name','id','name')->withPivot('content');
}
在配置文件模型中使用此类,我将无法获得所有属性。
当我在属性模型中使用 Profiles() 类时,我得到了每个属性,但不是用户的内容……而是每个用户的内容。
【问题讨论】:
-
不知道你在用那个
belongsToMany()做什么;它最多有 4 个属性:belongsToMany('Model', 'table', 'local_key', 'foreign_key');: "第三个参数是您定义关系的模型的外键名称,而第四个参数是您所在模型的外键名称加入:"
标签: php laravel eloquent relationship