Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"datalinx/php-utils": "^2.5",
"eclipsephp/common": "dev-main",
"filament/filament": "^3.3",
"filament/spatie-laravel-media-library-plugin": "^3.2",
"filament/spatie-laravel-translatable-plugin": "^3.2",
"spatie/laravel-package-tools": "^1.19",
"spatie/laravel-translatable": "^6.11"
Expand Down
21 changes: 21 additions & 0 deletions database/factories/ProductFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Eclipse\Catalogue\Factories;

use Eclipse\Catalogue\Models\Product;
use Exception;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Log;

class ProductFactory extends Factory
{
Expand Down Expand Up @@ -44,4 +46,23 @@ public function definition(): array
'updated_at' => Carbon::now(),
];
}

public function configure(): static
{
return $this->afterCreating(function (Product $product) {
$imageNumber = rand(1, 15);
$imagePath = storage_path("app/public/sample-products/{$imageNumber}.jpg");

if (file_exists($imagePath)) {
try {
$product->addMedia($imagePath)
->preservingOriginal()
->withCustomProperties(['is_cover' => true])
->toMediaCollection('images');
} catch (Exception $e) {
Log::warning("Failed to attach image to product {$product->id}: ".$e->getMessage());
}
}
});
}
}
42 changes: 42 additions & 0 deletions database/seeders/ProductSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,56 @@
namespace Eclipse\Catalogue\Seeders;

use Eclipse\Catalogue\Models\Product;
use Exception;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;

class ProductSeeder extends Seeder
{
public function run(): void
{
$this->ensureSampleImagesExist();

Product::factory()
->count(100)
->create();
}

private function ensureSampleImagesExist(): void
{
Storage::disk('public')->makeDirectory('sample-products');

$existingImages = Storage::disk('public')->files('sample-products');

if (count($existingImages) >= 15) {
$this->command->info('Sample images already exist.');

return;
}

$this->command->info('Downloading sample product images...');

for ($i = 1; $i <= 15; $i++) {
$imagePath = "sample-products/{$i}.jpg";

if (Storage::disk('public')->exists($imagePath)) {
continue;
}

try {
$imageUrl = "https://picsum.photos/400/300?random={$i}";
$response = Http::timeout(10)->get($imageUrl);

if ($response->successful()) {
Storage::disk('public')->put($imagePath, $response->body());
$this->command->info("Downloaded image {$i}/15");
}
} catch (Exception $e) {
$this->command->warn("Failed to download image {$i}: ".$e->getMessage());
}
}

$this->command->info('Sample images ready!');
}
}
Loading
Loading