Skip to content

Commit

Permalink
return models when reordering
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Jan 11, 2024
1 parent 1067381 commit 076beb2
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,17 +468,18 @@ public function getSrcset(?string $conversion = null): Collection

/**
* @param null|(Closure(null|string $previous): string) $sequence
* @return EloquentCollection<int, static>
*/
public static function reorder(array $keys, ?Closure $sequence = null, string $using = 'id'): void
public static function reorder(array $keys, ?Closure $sequence = null, string $using = 'id'): EloquentCollection
{
/** @var EloquentCollection<int, Media> */
/** @var EloquentCollection<int, static> */
$models = static::query()
->whereIn($using, $keys)
->get();

$models = $models->sortBy(function (Media $model) use ($keys, $using) {
return array_search($model->{$using}, $keys);
});
})->values();

$previous = $sequence ? null : -1;

Expand All @@ -491,5 +492,7 @@ public static function reorder(array $keys, ?Closure $sequence = null, string $u
$model->save();
}

return $models;

}
}

0 comments on commit 076beb2

Please sign in to comment.