Enhance ListingSeeder and LocationSeeder with improved data handling and structure

This commit is contained in:
fatihalp 2026-03-04 23:32:30 +03:00
parent 278e8063f3
commit 98c7167454
2 changed files with 0 additions and 181 deletions

View File

@ -1,48 +0,0 @@
<?php
namespace Modules\Listing\Database\Seeders;
use Illuminate\Database\Seeder;
use Modules\Category\Models\Category;
use Modules\Listing\Models\Listing;
class ListingSeeder extends Seeder
{
public function run(): void
{
$user = \App\Models\User::where('email', 'partner@openclassify.com')->first();
$categories = Category::where('level', 0)->get();
if (!$user || $categories->isEmpty()) return;
$listings = [
['title' => 'iPhone 14 Pro - Excellent Condition', 'price' => 799, 'city' => 'Istanbul', 'country' => 'Turkey'],
['title' => 'MacBook Pro 2023', 'price' => 1499, 'city' => 'Ankara', 'country' => 'Turkey'],
['title' => '2020 Toyota Corolla', 'price' => 18000, 'city' => 'New York', 'country' => 'United States'],
['title' => '3-Bedroom Apartment for Sale', 'price' => 250000, 'city' => 'Istanbul', 'country' => 'Turkey'],
['title' => 'Nike Running Shoes Size 42', 'price' => 89, 'city' => 'Berlin', 'country' => 'Germany'],
['title' => 'IKEA Dining Table', 'price' => 150, 'city' => 'London', 'country' => 'United Kingdom'],
['title' => 'Yoga Mat - Brand New', 'price' => 35, 'city' => 'Paris', 'country' => 'France'],
['title' => 'Web Developer for Hire', 'price' => 0, 'city' => 'Remote', 'country' => 'Turkey'],
['title' => 'Samsung 55" 4K TV', 'price' => 599, 'city' => 'Madrid', 'country' => 'Spain'],
['title' => 'Honda CBR500R Motorcycle 2021', 'price' => 6500, 'city' => 'Tokyo', 'country' => 'Japan'],
];
foreach ($listings as $i => $listing) {
$category = $categories->get($i % $categories->count());
Listing::firstOrCreate(
['slug' => \Illuminate\Support\Str::slug($listing['title']) . '-' . ($i + 1)],
array_merge($listing, [
'slug' => \Illuminate\Support\Str::slug($listing['title']) . '-' . ($i + 1),
'description' => 'This is a sample listing description for ' . $listing['title'],
'currency' => $listing['price'] > 0 ? 'USD' : 'USD',
'category_id' => $category?->id,
'user_id' => $user->id,
'status' => 'active',
'contact_email' => 'partner@openclassify.com',
'contact_phone' => '+1234567890',
'is_featured' => $i < 3,
])
);
}
}
}

View File

@ -1,133 +0,0 @@
<?php
namespace Modules\Listing\Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;
use Modules\Category\Models\Category;
use Modules\Listing\Models\Listing;
class ListingSeeder extends Seeder
{
public function run(): void
{
$user = \App\Models\User::where('email', 'b@b.com')
->orWhere('email', 'partner@openclassify.com')
->first();
$categories = Category::where('level', 0)->get();
if (!$user || $categories->isEmpty()) return;
$listings = [
[
'title' => 'iPhone 14 Pro 256 GB, temiz kullanılmış',
'description' => 'Cihaz sorunsuz çalışıyor, pil sağlığı iyi durumda. Kutusu ve şarj kablosu ile teslim edilecektir.',
'price' => 44999,
'city' => 'İstanbul',
'country' => 'Türkiye',
],
[
'title' => 'MacBook Pro M2 16 GB / 512 GB',
'description' => 'Yazılım geliştirme için kullanıldı. Kozmetik olarak çok iyi durumda, faturası mevcut.',
'price' => 62999,
'city' => 'Ankara',
'country' => 'Türkiye',
],
[
'title' => '2020 Toyota Corolla 1.6 Dream',
'description' => 'Boyalı parça yok, düzenli bakımlı aile aracı. Detaylı ekspertiz raporu paylaşılabilir.',
'price' => 980000,
'city' => 'İzmir',
'country' => 'Türkiye',
],
[
'title' => 'Bluetooth Kulaklık - Aktif Gürültü Engelleme',
'description' => 'Uzun pil ömrü ve net mikrofon performansı. Kutu içeriği tamdır.',
'price' => 3499,
'city' => 'Bursa',
'country' => 'Türkiye',
],
[
'title' => 'Masaüstü için 15 inç dizüstü bilgisayar',
'description' => 'Günlük kullanım ve ofis işleri için ideal. SSD sayesinde hızlıılış.',
'price' => 18450,
'city' => 'Antalya',
'country' => 'Türkiye',
],
[
'title' => 'Seramik Kahve Kupası Seti (6 Adet)',
'description' => 'Az kullanıldı, kırık/çatlak yok. Mutfak yenileme nedeniyle satılıktır.',
'price' => 650,
'city' => 'Adana',
'country' => 'Türkiye',
],
[
'title' => 'Sedan Araç - Düşük Kilometre',
'description' => 'Şehir içi kullanıldı, tüm bakımları zamanında yapıldı. Ciddi alıcılarla paylaşım yapılır.',
'price' => 845000,
'city' => 'Konya',
'country' => 'Türkiye',
],
];
$sampleImages = [
'sample_image/phone.jpeg',
'sample_image/macbook.jpg',
'sample_image/car.jpeg',
'sample_image/headphones.jpg',
'sample_image/laptop.jpg',
'sample_image/cup.jpg',
'sample_image/car2.jpeg',
];
$sampleImageFileNames = collect($sampleImages)
->map(fn (string $path): string => basename($path))
->values();
foreach ($listings as $i => $listing) {
$category = $categories->get($i % $categories->count());
$slug = Str::slug($listing['title']) . '-' . ($i + 1);
$listingModel = Listing::updateOrCreate(
['slug' => $slug],
array_merge($listing, [
'slug' => $slug,
'description' => $listing['description'],
'currency' => 'TRY',
'category_id' => $category?->id,
'user_id' => $user->id,
'status' => 'active',
'contact_email' => $user->email,
'contact_phone' => '+905551112233',
'is_featured' => $i < 3,
])
);
$imageRelativePath = $sampleImages[$i % count($sampleImages)];
$imageAbsolutePath = public_path($imageRelativePath);
if (! is_file($imageAbsolutePath)) {
continue;
}
$currentMedia = $listingModel->getMedia('listing-images');
$currentHasSampleImage = $currentMedia->contains(
fn ($media): bool => $sampleImageFileNames->contains((string) $media->file_name)
);
if (! $currentHasSampleImage) {
$listingModel->clearMediaCollection('listing-images');
}
$targetFileName = basename($imageAbsolutePath);
$alreadyHasTargetImage = $listingModel->getMedia('listing-images')
->contains(fn ($media): bool => (string) $media->file_name === $targetFileName);
if (! $alreadyHasTargetImage) {
$listingModel
->addMedia($imageAbsolutePath)
->preservingOriginal()
->toMediaCollection('listing-images');
}
}
}
}