Laravel-Backpack / CRUD

Build custom admin panels. Fast!
https://backpackforlaravel.com
MIT License
3k stars 880 forks source link

[Bug] Using orderLogic for a column is displaying wrong show/edit links (wrong ids) #5513

Closed dmarcos89 closed 2 months ago

dmarcos89 commented 2 months ago

Bug report

What I did

Applying a custom order for a column:

...
[
                'name' => 'freight_id',
                'label' => 'Freight',
                'type' => 'select',
                'entity' => 'freight',
                'orderable' => 'true',
                'orderLogic' => function ($query, $column, $columnDirection) {
                    return $query
                        ->leftJoin('freights', 'freights.id', '=', 'schedules.freight_id')
                        ->orderBy('freights.description', $columnDirection);
                },
                'searchLogic' => function ($query, $column, $searchTerm) {
                    $query->orWhereHas('freight', function ($q1) use ($searchTerm) {
                        $q1->where('description', 'like', '%'.$searchTerm.'%');
                    });
                },
            ],
...

What I expected to happen

Order the table using the selected column.

What happened

Is this expected? Any way to avoid this behaviour?

select
  *,
  (
    select
      count(*)
    from
      `offers`
    where
      `schedules`.`id` = `offers`.`schedule_id`
      and `offer_status_id` = 2
  ) as `offers_count`
from
  `schedules`
  left join `companies` on `companies`.`id` = `schedules`.`supplier_id`
where
  `schedules`.`deleted_at` is null
order by
  `companies`.`name` desc,
  schedules.id DESC,
  `datetime_to_consignee` asc
limit
  10

What I've already tried to fix it

Not sure what else to try...

Is it a bug in the latest version of Backpack?

After I run composer update backpack/crud the bug... is it still there?

Yes

Backpack, Laravel, PHP, DB version

### PHP VERSION:
PHP 8.2.8 (cli) (built: Jul 28 2023 13:55:05) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.8, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.8, Copyright (c), by Zend Technologies

### LARAVEL VERSION:
v10.23.1@dbfd495557678759153e8d71cc2f6027686ca51e

### BACKPACK PACKAGE VERSIONS:
backpack/crud: 5.6.1
backpack/filemanager: 2.0.1
backpack/pro: 1.6.6
karandatwani92 commented 2 months ago

Hey @dmarcos89

I'm using the following code in my project to handle it:

//PaymentCrudController
CRUD::column('booking_travel_datetime')
 ->type('select')
 ->entity('booking')
 ->model(\App\Models\Booking::class)
 ->label('Travel DateTime')
 ->attribute('booking_travel_datetime')
 ->orderable(true)
 ->orderLogic(function ($query, $column, $columnDirection) {
      return $query->leftJoin('bookings', 'bookings.id', '=', 'payments.booking_id')
      ->orderBy('bookings.booking_travel_datetime', $columnDirection)->select('payments.*');
 });
class Payment extends Model
{
    public function booking()
    {
       return $this->belongsTo(\App\Models\Booking::class, 'booking_id');
    }
}

I think ->select('schedules.*') can help!

dmarcos89 commented 2 months ago

Thank you so much @karandatwani92, that fixed it! 🥳