3

I'm having difficulty ordering the CRUD rows by a related value. In my main case I want to order jobs by the weight of their associated status

Job (table)->status_id ---> Status (table)->weight

I've got belongsTo and hasMany relationships setup correctly and add / edit etc, works well, I just can't figure out how to set the $this->crud->orderBy()

Divins Mathew
  • 2,908
  • 4
  • 22
  • 34

1 Answers1

3

It's no different than sorting any Laravel model by its relation. And I think the standard way is to use Laravel's eager loading for that:

$this->crud->query = $this->crud->query->with(['status' => function ($query) {
    $query->orderBy('weight', 'desc');
}])->get();

Hope it helps!

tabacitu
  • 6,047
  • 1
  • 23
  • 37
  • This worked for me, but in my case I had to remove the `->get()` to avoid `Too few arguments to function Illuminate\Support\Collection::get(), 0 passed in /home/vagrant/sites/bookit-nexxus/vendor/backpack/crud/src/PanelTraits/Read.php on line 102 and at least 1 expected` – Wesley Smith Jul 25 '19 at 15:20