79 lines
2.4 KiB
PHP
79 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Modules\Gallery\database\seeders;
|
|
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Database\Seeder;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Modules\Gallery\app\Models\Gallery;
|
|
use Modules\Gallery\app\Models\GalleryCategory;
|
|
|
|
class GalleryDatabaseSeeder extends Seeder
|
|
{
|
|
/**
|
|
* Run the database seeds.
|
|
*/
|
|
public function run(): void
|
|
{
|
|
//-- galleryCategories
|
|
$galleryCategories = [
|
|
'surgery',
|
|
'sample collection',
|
|
];
|
|
|
|
foreach ($galleryCategories as $category) {
|
|
GalleryCategory::create([
|
|
'uuid' => Str::uuid(),
|
|
'category' => $category,
|
|
'type' => 'image',
|
|
]);
|
|
}
|
|
|
|
|
|
|
|
//-- galleries
|
|
$galleries = [
|
|
['detail' => 'hair transplant 1', 'image' => 'g1.jpg'],
|
|
['detail' => 'hair transplant 2', 'image' => 'g2.jpg'],
|
|
['detail' => 'hair transplant 3', 'image' => 'g3.jpg'],
|
|
['detail' => 'hair transplant 4', 'image' => 'g4.jpg'],
|
|
['detail' => 'hair transplant 5', 'image' => 'g5.jpg'],
|
|
];
|
|
|
|
foreach ($galleries as $gallery) {
|
|
$galleryCategoriesIdInRandomOrder = GalleryCategory::all()->random()->id;
|
|
$cmsGallery = Gallery::create([
|
|
'uuid' => Str::uuid(),
|
|
'gallery_category_id' => $galleryCategoriesIdInRandomOrder,
|
|
'detail' => $gallery['detail'],
|
|
]);
|
|
|
|
// Add image to the created banner
|
|
$this->uploadImageForGallery($gallery['image'], $cmsGallery);
|
|
}
|
|
}
|
|
|
|
private function uploadImageForGallery(string $imageFileName, $cmsbanner)
|
|
{
|
|
$seederDirPath = 'galleries/';
|
|
|
|
// Generate a unique filename for the new image
|
|
$newFileName = Str::uuid() . '.jpg';
|
|
|
|
// Storage path for the new image
|
|
$storagePath = '/galleries/' . $newFileName;
|
|
|
|
// Check if the image exists in the seeder_disk
|
|
if (Storage::disk('seeder_disk')->exists($seederDirPath . $imageFileName)) {
|
|
// Copy the image from seeder to public
|
|
$fileContents = Storage::disk('seeder_disk')->get($seederDirPath . $imageFileName);
|
|
|
|
Storage::disk('public_uploads')->put($storagePath, $fileContents);
|
|
|
|
$cmsbanner->image = $newFileName;
|
|
$cmsbanner->image_path = $storagePath;
|
|
$cmsbanner->save();
|
|
}
|
|
}
|
|
}
|