110 lines
3.5 KiB
PHP
110 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace Modules\Transformation\database\seeders;
|
|
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Database\Seeder;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Modules\Transformation\app\Models\Transformation;
|
|
|
|
class TransformationDatabaseSeeder extends Seeder
|
|
{
|
|
/**
|
|
* Run the database seeds.
|
|
*/
|
|
public function run(): void
|
|
{
|
|
$transformations = [
|
|
[
|
|
'name' => 'Jane Smith',
|
|
'grade' => '05',
|
|
'graft' => '3,000',
|
|
'before' => 'before.png',
|
|
'after' => 'after.png',
|
|
],
|
|
[
|
|
'name' => 'Alex Johnson',
|
|
'grade' => '04',
|
|
'graft' => '2,800',
|
|
'before' => 'before.png',
|
|
'after' => 'after.png',
|
|
],
|
|
[
|
|
'name' => 'Emily Brown',
|
|
'grade' => '06',
|
|
'graft' => '3,200',
|
|
'before' => 'before.png',
|
|
'after' => 'after.png',
|
|
],
|
|
[
|
|
'name' => 'Michael Davis',
|
|
'grade' => '02',
|
|
'graft' => '2,200',
|
|
'before' => 'before.png',
|
|
'after' => 'after.png',
|
|
],
|
|
];
|
|
|
|
foreach ($transformations as $transformation) {
|
|
$transformationData = Transformation::create([
|
|
'name' => $transformation['name'],
|
|
'grade' => $transformation['grade'],
|
|
'graft' => $transformation['graft'],
|
|
]);
|
|
|
|
// Add image to the created banner
|
|
$this->uploadImageForBeforeTransformation($transformation['before'], $transformationData);
|
|
$this->uploadImageForAfterTransformation($transformation['after'], $transformationData);
|
|
}
|
|
}
|
|
|
|
|
|
//-- Upload Before transformation
|
|
private function uploadImageForBeforeTransformation(string $imageFileName, $transformation)
|
|
{
|
|
|
|
$seederDirPath = 'transformations/';
|
|
|
|
// Generate a unique filename for the new image
|
|
$newFileName = Str::uuid() . '.jpg';
|
|
|
|
// Storage path for the new image
|
|
$storagePath = '/transformations/' . $newFileName;
|
|
// $storagePath = $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);
|
|
|
|
$transformation->before = $newFileName;
|
|
$transformation->save();
|
|
}
|
|
}
|
|
|
|
//-- Upload After transformation
|
|
private function uploadImageForAfterTransformation(string $imageFileName, $transformation)
|
|
{
|
|
$seederDirPath = 'transformations/';
|
|
|
|
// Generate a unique filename for the new image
|
|
$newFileName = Str::uuid() . '.jpg';
|
|
|
|
// Storage path for the new image
|
|
$storagePath = '/transformations/' . $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);
|
|
|
|
$transformation->after = $newFileName;
|
|
$transformation->save();
|
|
}
|
|
}
|
|
}
|