Skip to content

Commit

Permalink
rename order column
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Jan 12, 2024
1 parent 3fc41d9 commit 7569bf1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
2 changes: 1 addition & 1 deletion database/migrations/create_media_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ return new class extends Migration
$table->string('disk')->nullable();
$table->unsignedBigInteger('size')->nullable();
$table->json('generated_conversions')->nullable();
$table->unsignedBigInteger('order')->nullable()->index();
$table->unsignedBigInteger('order_column')->nullable()->index();

$table->string('path')->nullable();
$table->string('type')->nullable();
Expand Down
6 changes: 3 additions & 3 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* @property ?int $height
* @property ?float $aspect_ratio
* @property ?string $average_color
* @property ?int $order
* @property ?int $order_column
* @property ?Collection<string, GeneratedConversion> $generated_conversions
* @property ?ArrayObject $metadata
* @property ?Model $model
Expand Down Expand Up @@ -485,9 +485,9 @@ public static function reorder(array $keys, ?Closure $sequence = null, string $u

foreach ($models as $model) {

$model->order = $sequence ? $sequence($previous) : ($previous + 1);
$model->order_column = $sequence ? $sequence($previous) : ($previous + 1);

$previous = $model->order;
$previous = $model->order_column;

$model->save();
}
Expand Down
16 changes: 5 additions & 11 deletions src/Traits/HasMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,14 @@
* @template TMedia of Media
*
* @property ?string $uuid
* @property EloquentCollection<int, TMedia> $media ordered by order, id
* @property EloquentCollection<int, TMedia> $unorderedMedia
* @property EloquentCollection<int, TMedia> $media ordered by order_column
*/
trait HasMedia
{
public function media(): MorphMany
{
return $this->morphMany(config('media.model'), 'model')
->orderBy('order')
->orderBy('id');
}

public function unorderedMedia(): MorphMany
{
return $this->morphMany(config('media.model'), 'model');
->orderByRaw('-order_column DESC');
}

/**
Expand All @@ -40,7 +33,8 @@ public function getMedia(?string $collection_name = null, ?string $collection_gr
{
return $this->media
->when($collection_name, fn (EloquentCollection $collection) => $collection->where('collection_name', $collection_name))
->when($collection_group, fn (EloquentCollection $collection) => $collection->where('collection_group', $collection_group));
->when($collection_group, fn (EloquentCollection $collection) => $collection->where('collection_group', $collection_group))
->values();
}

public function hasMedia(?string $collection_name = null, ?string $collection_group = null): bool
Expand Down Expand Up @@ -179,7 +173,7 @@ public function addMedia(
$media->model()->associate($this);

$media->collection_group = $collection_group;
$media->order = $order;
$media->order_column = $order;
$media->metadata = $metadata;

$media->storeFile(
Expand Down

0 comments on commit 7569bf1

Please sign in to comment.