Seeding into Modules with Media

Whenever you create a module with the twill artisan commands they usually also come with Seeders. My main question here is; Is there a way to also seed media(s) into modules when doing seeding? Can anyone guide me in the right direction for this? This could either be for a repeater or just the cover image.
2 Replies
pauldwight
pauldwight3mo ago
Yes you can. This should get you going - this is what I use on a Singleton Model where we have the following mediaParams set:
public $mediasParams = [
'cover' => [
'default' => [
[
'name' => 'default',
'ratio' => 16 / 9,
],
],
],
];
public $mediasParams = [
'cover' => [
'default' => [
[
'name' => 'default',
'ratio' => 16 / 9,
],
],
],
];
$repo = app(HomepageRepository::class);

$page = $repo->create([
'title' => 'Home page title',
'published' => true,
]);

// Handle Media
// 1. Get Disk
$disk = Config::get('twill.media_library.disk');
$filename = 'hero-image.png';
$filePath = database_path('seeders/images/') . $filename;
$uuid = Str::uuid();
// Get image size
list($w, $h) = getimagesize($filePath);

// Store image
Storage::disk($disk)->put($uuid . '/' . $filename, file_get_contents($filePath));
$media = Media::create([
'uuid' => $uuid . '/' . $filename,
'filename' => $filename,
'caption' => '',
'alt_text' => '',
'width' => $w,
'height' => $h,
]);

// Calculate the Ratio and handle crop params
$mediaRole = 'cover';
$mediaCrop = 'default';
$cropParams = $page->getMediasParams()[$mediaRole][$mediaCrop][0];
$originalRatio = $w / $h;
if ($cropParams['ratio'] === 0) {
$crop_w = $w;
$crop_h = $h;
$crop_x = 0;
$crop_y = 0;
} elseif ($originalRatio <= $cropParams['ratio']) {
$crop_w = $w;
$crop_h = floor($w / $cropParams['ratio']);
$crop_x = 0;
$crop_y = floor(($h - $crop_h) / 2);
} else {
$crop_w = floor($h * $cropParams['ratio']);
$crop_h = $h;
$crop_x = floor(($w - $crop_w) / 2);
$crop_y = 0;
}

// Attach media to model
$page->medias()->attach($media->id, [
'metadatas' => '{}',
'role' => $mediaRole,
'crop_x' => $crop_x,
'crop_y' => $crop_y,
'crop_w' => $crop_w,
'crop_h' => $crop_h,
'crop' => $mediaCrop,
'ratio' => $mediaCrop,
]);
$repo = app(HomepageRepository::class);

$page = $repo->create([
'title' => 'Home page title',
'published' => true,
]);

// Handle Media
// 1. Get Disk
$disk = Config::get('twill.media_library.disk');
$filename = 'hero-image.png';
$filePath = database_path('seeders/images/') . $filename;
$uuid = Str::uuid();
// Get image size
list($w, $h) = getimagesize($filePath);

// Store image
Storage::disk($disk)->put($uuid . '/' . $filename, file_get_contents($filePath));
$media = Media::create([
'uuid' => $uuid . '/' . $filename,
'filename' => $filename,
'caption' => '',
'alt_text' => '',
'width' => $w,
'height' => $h,
]);

// Calculate the Ratio and handle crop params
$mediaRole = 'cover';
$mediaCrop = 'default';
$cropParams = $page->getMediasParams()[$mediaRole][$mediaCrop][0];
$originalRatio = $w / $h;
if ($cropParams['ratio'] === 0) {
$crop_w = $w;
$crop_h = $h;
$crop_x = 0;
$crop_y = 0;
} elseif ($originalRatio <= $cropParams['ratio']) {
$crop_w = $w;
$crop_h = floor($w / $cropParams['ratio']);
$crop_x = 0;
$crop_y = floor(($h - $crop_h) / 2);
} else {
$crop_w = floor($h * $cropParams['ratio']);
$crop_h = $h;
$crop_x = floor(($w - $crop_w) / 2);
$crop_y = 0;
}

// Attach media to model
$page->medias()->attach($media->id, [
'metadatas' => '{}',
'role' => $mediaRole,
'crop_x' => $crop_x,
'crop_y' => $crop_y,
'crop_w' => $crop_w,
'crop_h' => $crop_h,
'crop' => $mediaCrop,
'ratio' => $mediaCrop,
]);
Strife
StrifeOP3mo ago
Oh nice! I will give it a go and report back on this. I appreciate your help. @pauldwight @pauldwight This worked brilliantly. Thank you!
Want results from more Discord servers?
Add your server