我正在尝试设置以下关系:
注释 (user_id(->belongTo
用户->belongsToMany
->组
(枢轴:group_user(
我希望能够获取与评论相关的所有组:Comment::find(i)->groups
. 还有Comment::wherehas('groups')
和Comment::Where(groups in [1,3])
我一直在看 https://github.com/staudenmeir/eloquent-has-many-deep#belongsto 但无法让它按预期工作。
public function groups()
{
return $this->hasManyDeep('AppGroup', ['group_user', 'AppUser']);
}
拉拉维尔 7
来自 github 讨论:
class Comment extends Model
{
use StaudenmeirEloquentHasManyDeepHasRelationships;
public function groups()
{
return $this->hasManyDeep(
Group::class
[User::class, 'group_user'],
['id'],
['user_id']
);
}
}
像魅力一样工作!
这可能会对你有所帮助
//'tables1','tables2','tables3' are relations in USER model & 'COL 7' is column of table 3
$x=USER::where('status','active')->with('tables1','tables2','tables3')->whereHas('tables3', function($q)
{
$q->where('COL 7',"US");
})->get();
dd($x);