Reputation: 312
I have issue in user model in laravel relation.i want to use or where but don't know how to use.please look at the code that i know it is wrong but don't know how to use correct one.
public function subUsers() {
return $this->hasMany(User::class,'level1_parent1','id')
->orWhere('level2_parent','id')
->orWhere('level3_parent','id')
->orWhere('level4_parent','id')
->orWhere('level5_parent','id')
->orWhere('level6_parent','id')
->orWhere('level7_parent','id');
}
Upvotes: -1
Views: 40
Reputation: 370
What you are tying to do with the orWhere is not possible. The orWhere method does not work in the way you are trying to use it when defining relationships. You have to wrap the condition into aditional closure.
I think this will work:
public function subUsers() {
return $this->hasMany(User::class)
->where(function ($query) {
$query->where('level1_parent', $this->id)
->orWhere('level2_parent', $this->id)
->orWhere('level3_parent', $this->id)
->orWhere('level4_parent', $this->id)
->orWhere('level5_parent', $this->id)
->orWhere('level6_parent', $this->id)
->orWhere('level7_parent', $this->id);
});
}
Upvotes: 1