Skip to content

Commit bf396d7

Browse files
committed
feat: add global scope for ordering
1 parent 37b6a6f commit bf396d7

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

src/Components/Forms/PageBuilder.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,6 @@ public function relationship(
326326
$this->loadStateFromRelationshipsUsing(function ($record, PageBuilder $component) {
327327
/** @var Collection */
328328
$blocks = $this->getConstrainAppliedQuery($record)
329-
->orderBy('order')
330329
->get();
331330

332331
$component->state($blocks->toArray());

src/Models/PageBuilderBlock.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace Redberry\PageBuilderPlugin\Models;
44

5+
use Illuminate\Database\Eloquent\Attributes\ScopedBy;
56
use Illuminate\Database\Eloquent\Concerns\HasUuids;
67
use Illuminate\Database\Eloquent\Model;
78
use Illuminate\Database\Eloquent\Relations\MorphTo;
89
use Redberry\PageBuilderPlugin\Abstracts\BaseBlock;
10+
use Redberry\PageBuilderPlugin\Models\Scopes\OrderScope;
911

1012
/**
1113
* @property string $id
@@ -14,6 +16,7 @@
1416
* @property string $page_builder_blockable_id
1517
* @property array $data
1618
*/
19+
#[ScopedBy(OrderScope::class)]
1720
class PageBuilderBlock extends Model
1821
{
1922
use HasUuids;

src/Models/Scopes/OrderScope.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Redberry\PageBuilderPlugin\Models\Scopes;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Scope;
8+
9+
class OrderScope implements Scope
10+
{
11+
public function apply(Builder $builder, Model $model): void
12+
{
13+
$builder->orderBy('order')->orderBy('created_at', 'desc');
14+
}
15+
}

0 commit comments

Comments
 (0)