72 lines
2.2 KiB
PHP
72 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace Modules\Banner\database\seeders;
|
|
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Database\Seeder;
|
|
use Modules\Banner\app\Models\Banner;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class BannerDatabaseSeeder extends Seeder
|
|
{
|
|
/**
|
|
* Run the database seeds.
|
|
*/
|
|
public function run(): void
|
|
{
|
|
$banners = [
|
|
[
|
|
'title' => 'Adventure Awaits',
|
|
'link' => 'https://jhigucms.com/adventure-awaits',
|
|
'ordering' => 1,
|
|
'caption' => 'Embark on an exciting journey with us. Discover new places and create lasting memories.',
|
|
'image' => '2.jpg'
|
|
],
|
|
[
|
|
'title' =>'Journey of Discovery',
|
|
'link' => 'https://jhigucms.com/adventure-awaits',
|
|
'ordering' => 2,
|
|
'caption' => 'Discover hidden gems and make every moment count on your travels.',
|
|
'image' => '1.jpg'
|
|
],
|
|
];
|
|
|
|
foreach ($banners as $banner) {
|
|
$cmsbanner = Banner::create([
|
|
'uuid' => Str::uuid(),
|
|
'title' => $banner['title'],
|
|
'link' => $banner['link'],
|
|
'ordering' => $banner['ordering'],
|
|
'caption' => $banner['caption'],
|
|
]);
|
|
|
|
// Add image to the created banner
|
|
$this->uploadImageForBanner($banner['image'], $cmsbanner);
|
|
}
|
|
}
|
|
|
|
|
|
private function uploadImageForBanner(string $imageFileName, $cmsbanner)
|
|
{
|
|
$seederDirPath = 'banners/';
|
|
|
|
// Generate a unique filename for the new image
|
|
$newFileName = Str::uuid() . '.jpg';
|
|
|
|
// Storage path for the new image
|
|
$storagePath = '/banners/' . $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();
|
|
}
|
|
}
|
|
}
|