Filtering table, second filter doesn't works

 ->filters([
                Filter::make('is_applicant')

                    ->toggle()
                    ->query(function (Builder $query) use ($userId) {
                        $query->where('user_id', $userId);
                    }),
                Filter::make('is_responsible')

                    ->toggle()
                    ->query(function (Builder $query) use ($userId) {
                        $query->whereHas('project.responsible', function ($query) use ($userId) {
                            $query->where('user_id', $userId);
                        });
                    })

the first filter shows only the requests created by me
the second filter should display only the requests for which i'm responsible trough a project (a request belongTo a project, a project has many responsibles (user) )

in the request model (rams)
  public function project()
    {
        return $this->belongsTo(RamsProject::class, 'project_id');
    }

in the project model
 public function responsible(): BelongsToMany
    {
        return $this->belongsToMany(User::class, 'rams_projects_users', 'project_id', 'user_id');
    }

    public function rams(): hasMany
    {
        return $this->hasMany(Rams::class, 'project_id');
    }
Was this page helpful?