rapyd-laravel
rapyd-laravel copied to clipboard
Order grid by relation and at the same time filter by relation
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.
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%'
I have a same problem. Filter has not working with columns format 'alias.column'
Same here.
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 ?
+1, the same problem
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
https://github.com/bretto36/rapyd-laravel/tree/relationship-bug-fix
This is the diff without the whitespace: https://github.com/zofe/rapyd-laravel/compare/master...bretto36:relationship-bug-fix?w=1
ping @zofe
Apologies that branch contains a bug. I am attempting to resolve the issue in a better way. Am open to suggestions
ok