Skip to content

Commit

Permalink
global scope order by id desc
Browse files Browse the repository at this point in the history
  • Loading branch information
fzldn committed Sep 16, 2024
1 parent f82110f commit ad8bd38
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
8 changes: 7 additions & 1 deletion app/Models/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

namespace App\Models;

use App\Models\Scopes\OrderByIdDesc;
use Illuminate\Database\Eloquent\Attributes\ScopedBy;
use Spatie\Activitylog\Models\Activity as ModelsActivity;

class Activity extends ModelsActivity {}
#[ScopedBy(OrderByIdDesc::class)]
class Activity extends ModelsActivity
{
//
}
18 changes: 18 additions & 0 deletions app/Models/Scopes/OrderByIdDesc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Models\Scopes;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

class OrderByIdDesc implements Scope
{
/**
* Apply the scope to a given Eloquent query builder.
*/
public function apply(Builder $builder, Model $model): void
{
$builder->orderBy($model->getKeyName(), 'desc');
}
}

0 comments on commit ad8bd38

Please sign in to comment.