【发布时间】:2015-05-23 01:09:30
【问题描述】:
我正在尝试制作一个应用程序,其中用户可以拥有可变数量的配置文件字段和数据。因此,站点管理员可以将 location 添加到字段列表中,然后所有具有 location 的用户在他们的模型中。
这是我的数据库
Table : users
Columns: id | name | email
Sample : 1 | Adam | example@example.com
Table : user_custom_field
Columns: id | slug | name
Sample : 1 | location | Location
Table : user_custom_data
Columns: id | user_id | field_id | data
Sample : 1 | 1 | 1 | United Kingdom
我已经尝试过诸如对 hasMany 进行后处理,但只能让它为 1 个结果工作,我不能让它为所有用户工作。
理想情况下,我想访问自定义字段,例如 location,例如:
$user->location
或
$user->custom->location
到目前为止,这是我的非工作模型......
public function custom()
{
$custom_data = $this->hasMany('App\User_Custom_Data', 'user_id', 'id')
->join('user_custom_field', 'user_custom_data.field_id', '=', 'user_custom_field.id');
return $custom_data;
}
我是不是走错了路?
【问题讨论】: