0

PLease, somebody, help me with filters. I have 3 tables: complaint, frontend_tag, frontendtags_complaints I heed to filter complaints by frontend_tag

here is my filter code:

$this->crud->addFilter([
            'name' => 'frontendtags',
            'type' => 'select2',
            'label'=> 'FE Tag'
        ], function() {
            return \App\Models\FrontendTags::all()->pluck('name', 'id')->toArray();
        }, function($value) {
            return $this->crud->query->whereHas('frontendtags', function ($q) use ($value) {
                $q->where('tag_id', $value);
            });
        });

here is my relation code:

 public function frontendtags()
    {
        return $this->belongsToMany('App\Models\FrontendTags', 'frontendtags_complaints', 'complaint_id', 'tag_id');
    }

The filter is not working.

  • Can you post the question on git or here https://gitter.im/BackpackForLaravel/Lobby#utm_source=notification&utm_medium=email&utm_campaign=unread-notifications pls? – Indra Apr 03 '17 at 11:42

1 Answers1

2

Replace

$q->where('tag_id', $value);

With

$q->where('frontend_tag.id', $value);

The where condition is applied on the related table, in this case frontend_tag.

Ghitu Ilie-Alin
  • 139
  • 1
  • 5
  • I got: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'frontend_tag.id' in 'where clause' (SQL: select count(*) as aggregate from `complaint` where exists (select * from `frontend_tags` inner join `frontendtags_complaints` on `frontend_tags`.`id` = `frontendtags_complaints`.`tag_id` where `frontendtags_complaints`.`complaint_id` = `complaint`.`id` and `frontend_tag`.`id` = 5) order by `id` desc) – Roman Zaglinskyi Apr 04 '17 at 09:17
  • 1
    It should be frontend_tags.id; You've specified the wrong name for the tags table so that is how I used it. – Ghitu Ilie-Alin Apr 04 '17 at 13:04