medialibrary-helper/tests/Models/Post.php

51 lines
1.5 KiB
PHP
Raw Normal View History

2023-03-07 15:31:24 +01:00
<?php
namespace Zoomyboy\MedialibraryHelper\Tests\Models;
use Illuminate\Database\Eloquent\Model;
2023-03-07 21:53:36 +01:00
use Illuminate\Support\Facades\Event;
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;
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaStored;
2023-03-07 15:31:24 +01:00
class Post extends Model implements HasMedia
{
use InteractsWithMedia;
public $guarded = [];
public function registerMediaCollections(): void
{
$this->addMediaCollection('defaultSingleFile')->singleFile();
$this->addMediaCollection('images');
2023-03-07 21:53:36 +01:00
$this->addMediaCollection('singleForced')->singleFile()->forceFileName(function($name) {
2023-03-07 15:31:24 +01:00
return $name.' '.now()->format('Y-m-d');
});
2023-03-07 21:53:36 +01:00
$this->addMediaCollection('multipleForced')->forceFileName(function($name) {
2023-03-07 15:31:24 +01:00
return $name.' '.now()->format('Y-m-d');
});
$this->addMediaCollection('singleStoringHook')->singleFile()->storing(function($adder, $fileName) {
return $adder->withCustomProperties([
'use' => 'AAA',
'ttt' => pathinfo($fileName, PATHINFO_FILENAME),
]);
});
2023-03-07 21:53:36 +01:00
$this->addMediaCollection('singleWithEvent')->singleFile()->stored(function(Media $media) {
Event::dispatch(new MediaStored($media));
});
$this->addMediaCollection('multipleFilesWithEvent')->stored(function(Media $media) {
Event::dispatch(new MediaStored($media));
});
2023-03-07 15:31:24 +01:00
}
}