Nayjest / Grids

Grids framework for Laravel
MIT License
203 stars 77 forks source link

Sorting with Callback #193

Open shyammtp opened 6 years ago

shyammtp commented 6 years ago

I have a issue with Eloquent Model with multiple table join and its column collected in a alias name. In the Query:

$query = Activity::from('activity_log as a');  
        $query->leftJoin('users as u','u.id','=','a.causer_id')
        ->select('a.id','a.log_name','a.description','u.name','a.created_at as created_at','a.properties'); 
(new FieldConfig)
                        ->setName('created_at')
                        ->setLabel('Date')
                        ->setCallback(function ($val, EloquentDataRow $row) {   
                            return $val;
                        })->setSortable(true)
                        ->setSorting(Grid::SORT_DESC)

I can't able to do the sortable on the created_at column, because it wont take alias a.created_at on the fieldname. I hope i can do this if i have Sortable Callback available on the library.

Please help me. How to solve this case?

wdog commented 6 years ago

Can you post the error? I have tons of aliases and they work.

shyammtp commented 6 years ago

Here it is,

BTW, I am using SQL Database If i set as like this

(new FieldConfig)
                        ->setName('created_at')
                        ->setLabel('Date')
                        ->setCallback(function ($val, EloquentDataRow $row) {   
                            return $val;
                        }) 
                        ->setSortable(true)
                        ->setSorting(Grid::SORT_DESC)

it throws: SQLSTATE[HY000]: General error: 20018 Ambiguous column name 'created_at'. [20018] (severity 16) [(null)] (SQL: select * from (select [a].[id], [a].[log_name], [a].[description], [u].[name], [a].[created_at], [a].[properties], row_number() over (order by [created_at] desc) as row_num from [activity_log] as [a] left join [dimasuser] as [u] on [u].[id] = [a].[causer_id]) as temp_table where row_num between 51 and 100 order by row_num)

But if i set like this:

(new FieldConfig)
                        ->setName('a.created_at')
                        ->setLabel('Date')
                        ->setCallback(function ($val, EloquentDataRow $row) {   
                            return $val; 
                        }) 
                        ->setSortable(true)
                        ->setSorting(Grid::SORT_DESC)

I cant able to get the value for created_at.

I have fixed this issue by overriding the Sorter.php class and added SortingCallback

(new FieldConfig)
                        ->setName('created_at')
                        ->setLabel('Date')
                        ->setCallback(function ($val, EloquentDataRow $row) {   
                            return $val;
                        })
                        ->setSortableCallback(function($provider, $sort) { 
                            $provider->orderBy('a.created_at',$sort[1]); 
                        }) 
                        ->setSortable(true)
                        ->setSorting(Grid::SORT_DESC)

Thanks, Please let me know if you have any suggestion on this.

wdog commented 6 years ago

the query in the first post is different from the second one. I contains dimasuser table and a select * from ( select ... ) and more...

can you post/check the query?

I think that if you define only 1 time the field created_at in the select there would be no problem.