Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support to ULID column type #3699

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/media-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
*/
'max_file_size' => 1024 * 1024 * 10, // 10MB

/*
* Determiner if the media table should use ULID instead of UUID.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: Typo in "Determine" 😄

*/
'use_ulid_column' => env('MEDIA_USE_ULID_COLUMN', false),

/*
* This queue connection will be used to generate derived and responsive images.
* Leave empty to use the default queue connection.
Expand Down
8 changes: 7 additions & 1 deletion database/migrations/create_media_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ return new class extends Migration
$table->id();

$table->morphs('model');
$table->uuid()->nullable()->unique();

if (config('media-library.use_ulid_column') === true) {
$table->ulid()->nullable()->unique();
} else {
$table->uuid()->nullable()->unique();
}

$table->string('collection_name');
$table->string('name');
$table->string('file_name');
Expand Down
5 changes: 5 additions & 0 deletions docs/installation-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ return [
* Adding a larger file will result in an exception.
*/
'max_file_size' => 1024 * 1024 * 10,

/*
* Determiner if the media table should use ULID instead of UUID.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: typo

*/
'use_ulid_column' => env('MEDIA_USE_ULID_COLUMN', false),

/*
* This queue connection will be used to generate derived and responsive images.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Spatie\MediaLibrary\MediaCollections\Models\Concerns;

trait DetermineUniqueIdentifierColumn
{
public static function determineUniqueIdentifierColumn(): string
{
return config('media-library.use_ulid_column') === true ? 'ulid' : 'uuid';
}
}
16 changes: 14 additions & 2 deletions src/MediaCollections/Models/Concerns/HasUuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ trait HasUuid
public static function bootHasUuid(): void
{
static::creating(function (Model $model) {
$column = self::determineUniqueIdentifierColumn();

/** @var \Spatie\MediaLibrary\MediaCollections\Models\Media $model */
if (empty($model->uuid)) {
$model->uuid = (string) Str::uuid();
if (empty($model->{$column})) {
$model->{$column} = $column === 'ulid' ? (string) Str::ulid() : (string) Str::uuid();
}
});
}
Expand All @@ -21,4 +23,14 @@ public static function findByUuid(string $uuid): ?Model
{
return static::where('uuid', $uuid)->first();
}

public static function findByUlid(string $ulid): ?Model
{
return static::where('ulid', $ulid)->first();
}

public static function findBy(string $unique): ?Model
{
return static::where(self::determineUniqueIdentifierColumn(), $unique)->first();
}
}
3 changes: 3 additions & 0 deletions src/MediaCollections/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Spatie\MediaLibrary\MediaCollections\HtmlableMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Collections\MediaCollection;
use Spatie\MediaLibrary\MediaCollections\Models\Concerns\CustomMediaProperties;
use Spatie\MediaLibrary\MediaCollections\Models\Concerns\DetermineUniqueIdentifierColumn;
use Spatie\MediaLibrary\MediaCollections\Models\Concerns\HasUuid;
use Spatie\MediaLibrary\MediaCollections\Models\Concerns\IsSorted;
use Spatie\MediaLibrary\ResponsiveImages\RegisteredResponsiveImages;
Expand All @@ -39,6 +40,7 @@

/**
* @property string $uuid
* @property string $ulid
* @property string $model_type
* @property string|int $model_id
* @property string $collection_name
Expand All @@ -64,6 +66,7 @@
class Media extends Model implements Attachable, Htmlable, Responsable
{
use CustomMediaProperties;
use DetermineUniqueIdentifierColumn;
use HasUuid;
use IsSorted;

Expand Down