1

I have a list of record.

Its model has this

protected static function booted()
{
    static::addGlobalScope(new OrderByDescriptionScope);
}

this scope is doing

public function apply(Builder $builder, Model $model)
{
    $builder->orderBy('descrizione', 'asc');
}

but when opening a list, records are never sorted by descrizione.

This is the suggested method of apply a global scope, as for laravel 8 documentation.

Why does my code not work?

How to set default sorting for laravel backpack?

realtebo
  • 23,922
  • 37
  • 112
  • 189

2 Answers2

1

using crud object, you can sort your recodes using orderBy method, for example:

 public function setupListOperation()
    {
    $this->crud->orderBy('created_at', 'desc');
}
OMR
  • 11,736
  • 5
  • 20
  • 35
-1
    $collect_all = $collect_all->sortByDesc("id");

    $collect_all->values()->all();

when you fetch data after order like this.

Hasan Çetin
  • 172
  • 1
  • 9
  • Is this specific to backpack for Laravel? if yes, where do I put this code? and why my code doesn't work? - Please note : i do not downvoted your answer. – realtebo Oct 22 '21 at 14:53
  • when you get data if it's like $product = Product::query()->where("id", "=", 3)->get(); after $product = $product->sortByDesc("id"); $product->values->all(); /// it'is simple query for example. you can use this method complex queries. – Hasan Çetin Oct 22 '21 at 20:28