laravel-livewire-tables icon indicating copy to clipboard operation
laravel-livewire-tables copied to clipboard

Relationship sorting issue

Open nocodelab opened this issue 3 years ago • 2 comments

Hello guys, I'm experiencing an issue with the relationship sorting.

In my case I have:

  • Order model (various fields including order_status_id)
  • Order status model (id, name)

The Order model has a relationship method status() to retrieve the order's status.

I have set up the column for the datatable in this way:


public function columns(): array
    {
        return [
            Column::make("ID", "id")
                ->format(
                    fn($value, $row, Column $column) => '<strong>#'.$row->id.'</strong>'
                )
                ->html()
                ->sortable(),
            Column::make("Order status id", "status.name")
                ->searchable()
                ->sortable(),
            ]
}

My issue is that the resulting sort is wrong. Debugging the query in Debug bar I see that the generated query is the below:

SELECT
	`orders`.`id` AS `id`,
	`order_statuses`.`name` AS `status.name`
FROM
	`orders`
	LEFT JOIN `order_statuses` ON `orders`.`order_status_id` = `order_statuses`.`id`
ORDER BY
	"status.name" ASC
LIMIT 100 OFFSET 0

I believe that the error is related to the LIMIT/OFFSET in the query. Should I use a custom sortable function with a subquery?

nocodelab avatar Sep 15 '22 09:09 nocodelab

I think the issue is the " " around "status.name". If you changed the "status.name" to status.name the query will work in mySql, but doesn't work in the datatable.

Rich594 avatar Sep 16 '22 15:09 Rich594

I think the issue is the " " around "status.name". If you changed the "status.name" to status.name the query will work in mySql, but doesn't work in the datatable.

Well, not actually. In that case the sort will be correct but If the LIMIT is smaller than the total record it will display just a chunk of the data.

See the examples below: Screenshot 2022-09-16 at 17 44 01

Screenshot 2022-09-16 at 17 44 21

nocodelab avatar Sep 16 '22 15:09 nocodelab

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Oct 17 '22 06:10 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 24 '22 03:11 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jan 03 '23 20:01 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Feb 10 '23 21:02 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Mar 18 '23 21:03 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 20 '23 12:04 stale[bot]