medialibrary-helper/tests/workbench/app/Models/Post.php

67 lines
2.5 KiB
PHP
Raw Normal View History

2023-03-07 15:31:24 +01:00
<?php
2024-01-03 11:10:59 +01:00
namespace Workbench\App\Models;
2023-03-07 15:31:24 +01:00
use Illuminate\Database\Eloquent\Model;
2023-03-07 21:53:36 +01:00
use Illuminate\Support\Facades\Event;
2023-03-07 23:11:35 +01:00
use Illuminate\Support\Str;
2023-03-07 15:31:24 +01:00
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
2023-03-07 21:53:36 +01:00
use Spatie\MediaLibrary\MediaCollections\Models\Media;
2024-01-03 11:10:59 +01:00
use Workbench\App\Events\MediaChange;
use Workbench\App\Events\MediaDestroyed;
use Workbench\App\Events\MediaStored;
2023-03-07 15:31:24 +01:00
class Post extends Model implements HasMedia
{
use InteractsWithMedia;
public $guarded = [];
public function registerMediaCollections(): void
{
2023-05-02 14:23:25 +02:00
$this->addMediaCollection('defaultSingleFile')->maxWidth(fn () => 250)->singleFile();
2023-03-07 15:31:24 +01:00
2023-03-08 00:58:15 +01:00
$this->addMediaCollection('conversionsWithDefault')
2024-01-02 01:28:49 +01:00
->singleFile()
->withFallback(fn ($parent) => ['default.jpg', 'public'])
->registerMediaConversions(function () {
$this->addMediaConversion('tiny')->width(200)->height(200);
});
2023-03-08 00:58:15 +01:00
2023-03-13 11:05:05 +01:00
$this->addMediaCollection('images')->after(fn ($model) => Event::dispatch(new MediaChange($model)));
2023-03-07 15:31:24 +01:00
2023-03-08 01:38:57 +01:00
$this->addMediaCollection('singleForced')->singleFile()->forceFileName(function ($model, $name) {
2024-01-02 01:28:49 +01:00
return $name . ' ' . now()->format('Y-m-d');
2023-03-07 15:31:24 +01:00
});
2023-03-08 01:38:57 +01:00
$this->addMediaCollection('multipleForced')->forceFileName(function ($model, $name) {
2024-01-02 01:28:49 +01:00
return $name . ' ' . now()->format('Y-m-d');
2023-03-07 15:31:24 +01:00
});
2023-03-07 23:11:35 +01:00
$this->addMediaCollection('singleStoringHook')->singleFile()->storing(function ($adder, $fileName) {
2023-03-07 15:31:24 +01:00
return $adder->withCustomProperties([
'use' => 'AAA',
'ttt' => pathinfo($fileName, PATHINFO_FILENAME),
]);
});
2023-03-07 21:53:36 +01:00
2023-03-07 23:11:35 +01:00
$this->addMediaCollection('singleWithEvent')->singleFile()->stored(function (Media $media) {
2023-03-07 21:53:36 +01:00
Event::dispatch(new MediaStored($media));
2023-03-13 10:17:14 +01:00
})
2024-01-02 01:28:49 +01:00
->destroyed(fn ($model) => Event::dispatch(new MediaDestroyed($model)))
->after(fn ($model) => Event::dispatch(new MediaChange($model)));
2023-03-07 21:53:36 +01:00
2023-03-07 23:11:35 +01:00
$this->addMediaCollection('multipleFilesWithEvent')->stored(function (Media $media) {
2023-03-07 21:53:36 +01:00
Event::dispatch(new MediaStored($media));
});
2023-03-07 15:31:24 +01:00
2023-03-07 23:11:35 +01:00
$this->addMediaCollection('multipleProperties')->singleFile()->withDefaultProperties(fn ($path) => [
'test' => Str::camel($path),
])->withPropertyValidation(fn ($path) => [
'test' => 'string|max:10',
2023-03-13 10:34:52 +01:00
])
2024-01-02 01:28:49 +01:00
->after(fn ($model) => Event::dispatch(new MediaChange($model)));
2023-03-07 23:11:35 +01:00
}
2023-03-07 15:31:24 +01:00
}