我有两个用户角色。在数据库中,我为两个角色( model_profiles和client_profiles)都有自己的概要表。是否有任何方法动态地设置相关的配置文件模型?我试过这个:
//User model:
public function profile(){
return $this->role == 'model' ? $this->hasOne('App\Models\Model\Profile') : $this->hasOne('App\Models\Client\Profile');
}但是在这种情况下,我不能使用一些有说服力的方法,比如User::with('profile')->whereIn('id', [1,2,3])->get();,因为在加载用户模型之前,$this->role是null。
有没有任何方法可以使用基于用户角色的配置文件模型,并且不会丢失任何有说服力的查询方法?
发布于 2018-12-17 13:25:27
您可以使用多态关系(更多信息)
https://stackoverflow.com/questions/53815944
复制相似问题