rapyd-laravel icon indicating copy to clipboard operation
rapyd-laravel copied to clipboard

Order grid by relation and at the same time filter by relation

Open RichardLindhout opened this issue 9 years ago • 10 comments

Guys! I have a problem. I can now sort by relationship by joining the tables and give aliases to the columns, so I don't have a problem with columns with the same names.

But I need to get the DataFilter working. (The syntax of the code is a little bit different because I use LaravelPanel.)

This doesn't work

  $this->filter->add('brand_name', 'Merk ', 'text');
  $this->filter->add('brand_type_name', 'Model', 'text');

The code beneath gives a exception

    $this->filter->add('brand.name', 'Merk ', 'text'); //this does not do anything
    $this->filter->add('brand_type.name', 'Model', 'text'); //this gives an exception

Exception:

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'name' in where clause is ambiguous (SQL: select count(*) as aggregate from `tire` left join `brand` on `brand`.`id` = `tire`.`brand_id` left join `brand_type` on `brand_type`.`id` = `tire`.`brand_type_id` left join `season`     on `season`.`id` = `tire`.`season_id` where `name` LIKE %elite% and `name` LIKE %elite%)

Full code:

$this->filter = \DataFilter::source(\DB::table('tire')
        ->leftJoin('brand', 'brand.id', '=', 'tire.brand_id')
        ->leftJoin('brand_type', 'brand_type.id', '=', 'tire.brand_type_id')
        ->leftJoin('season', 'season.id', '=', 'tire.season_id')->select('tire.*','brand.name as brand_name', 'brand_type.name as brand_type_name','season.name as season_name') );

    $this->filter->add('tire_number', 'Nummer', 'text');
    $this->filter->add('brand_name', 'Merk ', 'text');
    $this->filter->add('brand_type_name', 'Model', 'text');

    $this->filter->add('brand.name', 'Merk ', 'text');
    $this->filter->add('brand_type.name', 'Model', 'text');

    //$this->filter->add('name', 'Merk', 'text');
    //$this->filter->reset('X');
    $this->filter->reset('Filters verwijderen');
    $this->filter->submit('Zoeken');

    $this->filter->build();

    $this->grid = \DataGrid::source($this->filter);


   // $brandName = $entity->brand()->getBrandName($entity->id);
    $this->grid->add('tire_number','Nummer',true);
    $this->grid->add('brand_name','Merk',true);
    $this->grid->add('brand_type_name','Model',true);
    $this->grid->add('season_name','Seizoen',true);

Does anybody know the solution to this problem.

RichardLindhout avatar Jan 21 '16 11:01 RichardLindhout

The query should be:

select count(*) as aggregate from `tire` left join `brand` on `brand`.`id` = `tire`.`brand_id` left join `brand_type` on `brand_type`.`id` = `tire`.`brand_type_id` left join `season` on `season`.`id` = `tire`.`season_id` where `brand_type`.`name` LIKE '%elite%'

RichardLindhout avatar Jan 21 '16 12:01 RichardLindhout

I have a same problem. Filter has not working with columns format 'alias.column'

galimru avatar Feb 21 '16 10:02 galimru

Same here.

TBarina avatar Mar 01 '16 22:03 TBarina

Is there any fix for this ? This is a really serious problem. I do not see how can this grid can be used in a real world application if you cannot filter by a related column that has the same name.. What is the reason we cannot use column aliases in filters ?

mberende avatar May 26 '16 09:05 mberende

+1, the same problem

biakaveron avatar Sep 27 '16 08:09 biakaveron

Same here. I was able to make it work by changing the code. I will put a branch in for my own code and let people see it

bretto36 avatar Feb 06 '17 04:02 bretto36

https://github.com/bretto36/rapyd-laravel/tree/relationship-bug-fix

bretto36 avatar Feb 06 '17 05:02 bretto36

This is the diff without the whitespace: https://github.com/zofe/rapyd-laravel/compare/master...bretto36:relationship-bug-fix?w=1

ping @zofe

tacone avatar Feb 06 '17 11:02 tacone

Apologies that branch contains a bug. I am attempting to resolve the issue in a better way. Am open to suggestions

bretto36 avatar Feb 07 '17 03:02 bretto36

ok

tacone avatar Feb 07 '17 13:02 tacone