I am elaborating code acquired here ManyToMany relation - how update attribute in pivot table
what I want:
I want to get a collection of Activities
related to any weekly Routine
. The pivot table's atribute done_at
tells me when any activity (task) was completed.
I can list and later ->count()
activities related to parent model in manyToMany relation:
public function activities()
{
return $this->belongsToMany('App\Models\Activity', 'activity_routine', 'routine_id', 'activity_id')->withPivot('done_at')->withTimestamps();
}
Now I want to get a collection of activities, which are not yet done. They have a pivot attribute done_at
set to null
.
my attempt:
I wish the below code worked. Unfortunately it doesn't.
I get error Illegal operator
. When in stead of '!='
I put simply '='
, the code works like a dream, but it gives me list of Activities already done.
Any hints?
public function activitiesOnlyDone()
{
return $this->belongsToMany('App\Models\Activity')->withPivot('done_at')->wherePivot('done_at','!=',null);
}
Additional hints: getting the value of an extra pivot table column laravel