LifeOnScreen / nova-sort-relations

This package improves support for sorting relations in Laravel Nova.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cant figure out how to get this package to work

timcv opened this issue · comments

I have added to my resource:

public static $sortRelations = [
        'dealer'          => 'dealers.name',
    ];

When i click the column for my belongs to field the key inte url is set to 'dealer_id'.

This mean that that the array_key_exists in the foreach in applyOrderings will search for a column named 'delaer_id' while my array key in $sortRelations is set to 'dealer'.

I understand that i need to modify the index query and join the dealer table to my resource.

Do you have any idea about what im missing?
Is this suposed to work with belongsTo fields?

@timcv

Not sure if my comment is still relevant here.

The key in the $sortRelations array is the name of the relation you're trying to sort on and the value is the column of that relation. If you then join the table in indexQuery it should work.

If that doesn't work you may need to provide more info.

And yes, it works with BelongsTo fields.

@RVxLab does not seem to work, I have a products.color_id, trying to sort by colors's name.
I have a function color() in Product.php model but the SQL is invalid. What did I do wrong?

public static $sortRelations = [
    'color' => 'colors.name',    // doesn't sort by name, it sorts by color_id only
];
# generates invalid SQL
select * from `products` order by `colors`.`name` desc limit 26 offset 0          

@timcv I had to join the colors table myself inside indexQuery(). Works now, but I was expecting something easier. Thanks anyway @RVxLab