From 179038413543b06c1688350be41d77b8cbf4c72d Mon Sep 17 00:00:00 2001 From: AbdelrahmanBl Date: Mon, 11 Dec 2023 17:34:42 +0200 Subject: [PATCH] [MOD] add modifyQuery with a callback instead of withoutGlobalScope for setting a new order --- src/SortableTrait.php | 6 ++++-- tests/SortableTest.php | 4 +++- tests/TestCase.php | 5 ++++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/SortableTrait.php b/src/SortableTrait.php index 1249e8c..f5cd4c3 100644 --- a/src/SortableTrait.php +++ b/src/SortableTrait.php @@ -40,7 +40,7 @@ public function scopeOrdered(Builder $query, string $direction = 'asc') return $query->orderBy($this->determineOrderColumnName(), $direction); } - public static function setNewOrder($ids, int $startOrder = 1, string $primaryKeyColumn = null, array $withoutGlobalScopes = []): void + public static function setNewOrder($ids, int $startOrder = 1, string $primaryKeyColumn = null, callable $modifyQuery = null): void { if (! is_array($ids) && ! $ids instanceof ArrayAccess) { throw new InvalidArgumentException('You must pass an array or ArrayAccess object to setNewOrder'); @@ -56,7 +56,9 @@ public static function setNewOrder($ids, int $startOrder = 1, string $primaryKey foreach ($ids as $id) { static::withoutGlobalScope(SoftDeletingScope::class) - ->withoutGlobalScopes($withoutGlobalScopes) + ->when(is_callable($modifyQuery), function($query) use ($modifyQuery) { + return $modifyQuery($query); + }) ->where($primaryKeyColumn, $id) ->update([$orderColumnName => $startOrder++]); } diff --git a/tests/SortableTest.php b/tests/SortableTest.php index c790bc9..6ad83e1 100644 --- a/tests/SortableTest.php +++ b/tests/SortableTest.php @@ -85,7 +85,9 @@ public function it_can_set_new_order_without_global_scopes_models() $newOrder = Collection::make(Dummy::all()->pluck('id'))->shuffle()->toArray(); - DummyWithGlobalScope::setNewOrder($newOrder, 1, null, ['ActiveScope']); + DummyWithGlobalScope::setNewOrder($newOrder, 1, null, function($query) { + $query->withoutGlobalScope('ActiveScope'); + }); foreach (Dummy::orderBy('order_column')->get() as $i => $dummy) { $this->assertEquals($newOrder[$i], $dummy->id); diff --git a/tests/TestCase.php b/tests/TestCase.php index 3c92594..203f6d1 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -3,6 +3,7 @@ namespace Spatie\EloquentSortable\Test; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\Schema; use Orchestra\Testbench\TestCase as Orchestra; abstract class TestCase extends Orchestra @@ -31,7 +32,7 @@ protected function getPackageProviders($app) */ protected function getEnvironmentSetUp($app) { - $app['config']->set('database.default', 'sqlite'); + $app['config']->set('database.default', 'mysql'); $app['config']->set('database.connections.sqlite', [ 'driver' => 'sqlite', 'database' => ':memory:', @@ -41,6 +42,8 @@ protected function getEnvironmentSetUp($app) protected function setUpDatabase() { + Schema::dropIfExists('dummies'); + $this->app['db']->connection()->getSchemaBuilder()->create('dummies', function (Blueprint $table) { $table->increments('id'); $table->string('name');