1

So laravel eloquent is using id asc as default in every query ... which is a big problem for me because in most cases i need data to be sorted by id desc

And i dont think its just me ... you almost always want new rows to show in the top of the list in the view .

Whether its list of news or users or a blog posts .

Of curse i can use ->orderBy('id' , 'desc') but if i have to write this for every single query i think i would need to see a shrink after each project !

So i'm looking for a clean way and without a need to add something to each model ... apparently there is Global Scopes but i need to add id to each model which i'm ging to consider it as plan B .

So , im looking to change this globally for each and every query ... is there a way or should i go with plan B!?

max
  • 3,614
  • 9
  • 59
  • 107

3 Answers3

1

For your purpose you can use a custom BaseClass which will define the default orderBy property to desc and then you can Extend your Models to this class instead of Model class.

Custom BaseClass:

class BaseClass extends Model{
protected static function boot() {
    parent::boot();
    static::addGlobalScope('order', function (Builder $builder) {
        $builder->orderBy($builder->column,'desc');
    });
  }
}

Then all the Models where you want to achieve order by desc, extend it with this BaseClass:
Your Model:

class abcModel extends BaseModel{
//Your code
}

To remove the default behavior just use:

abcModel::withoutGlobalScope('order')->get();
jaysingkar
  • 4,315
  • 1
  • 18
  • 26
0

Can't you just simply make your own query function and have it call ->orderBy('id' , 'desc')? I can't think of any other solution.

Mat VP
  • 29
  • 2
-4

You can change Laravels default orderBy

/**
 * Add an "order by" clause to the query.
 *
 * @param  string  $column
 * @param  string  $direction
 * @return $this
 */
public function orderBy($column, $direction = 'asc')
{
    $this->{$this->unions ? 'unionOrders' : 'orders'}[] = [
        'column' => $column,
        'direction' => strtolower($direction) == 'asc' ? 'asc' : 'desc',
    ];
    return $this;
}

In /vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php

Change:

public function orderBy($column, $direction = 'asc')

to:

public function orderBy($column, $direction = 'desc')

@Grzegorz Gajda is correct though, you shouldn't edit Laravel Code. Best solution is to use Laravel as it was designed, when you want descending:

->orderBy('id' , 'desc');
cmac
  • 3,123
  • 6
  • 36
  • 49
  • 5
    You shouldn't edit external dependencies especially Laravel code. What happen when somebody would want to update Laravel to newer version? – Grzegorz Gajda Aug 04 '16 at 19:34
  • @cmac as you said it's not right to edit laravel code , but in the other hand this is what i want since apparently there is no out of box solution – max Aug 04 '16 at 20:24
  • @max long as you are aware of the implications and don't forget if you ever update laravel, etc, etc... Glad I could help you. If this answer solves your problem please mark it as the answer. – cmac Aug 04 '16 at 20:33