moonshine-software / moonshine

Laravel Admin panel and more. Simple for beginners and powerful for experts. Using Blade, Alpine.js and Tailwind CSS.
https://moonshine-laravel.com
MIT License
714 stars 94 forks source link

Sorting on pages by columns different then "id" don't works #1183

Closed twent closed 1 month ago

twent commented 1 month ago

MoonShine Version

2.20.4

Laravel Version

11.20.0

PHP Version

8.3.10

Database Driver & Version

MariaDB 10.11.7

Description

Sorting on pages by columns different then "id" doesn't work with resource IndexPage and throws error sorting-error After deleting setColumn method then don't throws error, but sorting works by 'id'

Current solution: duplicate fields with sortable callback directly in fields method of resource.

Steps To Reproduce

  1. AssetQueryBuilder methods

    public function orderByEmployeeName($direction = 'asc'): self
    {
        return $this->select('assets.*')
            ->join('employees', 'assets.employee_id', '=', 'employees.id')
            ->orderBy('employees.name', $direction);
    }
    
    public function orderByCategoryName($direction = 'asc'): self
    {
        return $this->select('assets.*')
            ->join('asset_categories', 'assets.asset_category_id', '=', 'asset_categories.id')
            ->orderBy('asset_categories.name', $direction);
    }
  2. Method fields in AssetIndexPage. Sorting by Employee.name and AssetCategory.name don't works
    public function fields(): array
    {
        return [
            Text::make('№', 'inventory_number')->sortable(),
            BelongsTo::make(
                'Ответственный',
                'employee',
                formatted: fn ($item) => "<span><b>{$item->name}</b><br>{$item->organization->name}</span>"
            )->sortable(
                fn (AssetQueryBuilder $query, string $column, string $direction) =>
                    $query->orderByEmployeeName($direction)
            )->setColumn('employee'),
            Text::make('Объект учёта', 'name', fn ($item) => $item->getExcerptPrintName())
                ->setColumn('asset_category_id')
                ->sortable(
                    fn (AssetQueryBuilder $query, string $column, string $direction) =>
                        $query->orderByCategoryName($direction)
                )->setColumn('category'),
            Text::make('Производитель', 'manufacturer')->sortable(),
            // other fields
        ];
    }
  3. Adding fields method to AssetResouce. Now sorting works fine
    public function fields(): array
    {
        return [
            BelongsTo::make(
                'Ответственный',
                'employee',
                formatted: fn ($item) => "<span><b>{$item->name}</b><br>{$item->organization->name}</span>"
            )->sortable(
                fn (AssetQueryBuilder $query, string $column, string $direction) =>
                    $query->orderByEmployeeName($direction)
            )->setColumn('employee'),
            Text::make('Объект учёта', 'name', fn ($item) => $item->getExcerptPrintName())
                ->setColumn('asset_category_id')
                ->sortable(
                    fn (AssetQueryBuilder $query, string $column, string $direction) =>
                    $query->orderByCategoryName($direction)
                )->setColumn('category'),
        ];
    }