adrema/database/factories/Traits/FakesMedia.php

43 lines
1.5 KiB
PHP
Raw Permalink Normal View History

2024-01-14 15:52:54 +01:00
<?php
namespace Database\Factories\Traits;
use Illuminate\Http\UploadedFile;
use Spatie\MediaLibrary\HasMedia;
trait FakesMedia
{
2024-02-09 00:21:33 +01:00
public function withImage(string $collection, string $filename): self
2024-01-14 15:52:54 +01:00
{
return $this->afterCreating(function (HasMedia $model) use ($filename, $collection) {
$pathinfo = pathinfo($filename);
UploadedFile::fake()->image($filename, 1000, 1000)->storeAs('media-library', $filename, 'temp');
$model->addMediaFromDisk('media-library/' . $filename, 'temp')
->usingName($pathinfo['filename'])
->usingFileName($pathinfo['basename'])
->toMediaCollection($collection);
});
}
2024-04-24 15:39:07 +02:00
/**
* @param array<string, mixed> $properties
*/
2024-04-23 23:48:09 +02:00
public function withDocument(string $collection, string $filename, string $content = '', array $properties = []): self
{
2024-04-23 23:48:09 +02:00
return $this->afterCreating(function (HasMedia $model) use ($filename, $collection, $content, $properties) {
$pathinfo = pathinfo($filename);
UploadedFile::fake()->create($filename, $content, 'application/pdf')->storeAs('media-library', $filename, 'temp');
$model->addMediaFromDisk('media-library/' . $filename, 'temp')
->usingName($pathinfo['filename'])
->usingFileName($pathinfo['basename'])
2024-04-23 23:48:09 +02:00
->withCustomProperties($properties)
->toMediaCollection($collection);
});
}
2024-01-14 15:52:54 +01:00
}