My application has Users and Groups. They are related with a pivot table. The pivot table has nullable "joined" and "left" datetimes.
How do I use Logical Grouping on a pivot table?
I've tried:
$this->belongsToMany(User::class)->wherePivot(function ($query) use ($time) {
$query->where('joined', null)->orWhere('joined', '<=', $time);
})
// and "left"
;
and
$this->belongsToMany(User::class)->where(function ($query) use ($time) {
$query->wherePivot('joined', null)->orWherePivot('joined', '<=', $time);
})
// and "left"
;
but neither work. The first complains about passing a function as an argument that's expected to be a string, and the second complains about wherePivot
not being defined.
Am I missing something obvious?
if you are in the Group model you can define:
public function joinedbefore()
{
return $this->belongsToMany(User::class)->withPivot(['your pivot-columns'])->where(function ($query) {
$query->where('joined', null);//orWhere...
});
}
Or:
public function joinedbefore()
{
return $this->belongsToMany(User::class)->wherePivot('joined', null)->orWherePivot('joined', '<=',$var);
}
Then it can be called:
$group = Group::find(1);//or any other way
$group->joinedbefore()->get();