D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
forge
/
ghanempharmacy.com
/
database
/
seeders
/
Filename :
GiftConfigurationSeeder.php
back
Copy
<?php namespace Database\Seeders; use Illuminate\Database\Seeder; use App\Models\GiftConfiguration; use App\Models\Product; use Carbon\Carbon; use Illuminate\Support\Str; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; class GiftConfigurationSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { // Get 10 random products that are active and not rejected $productQuery = Product::where('status', 1)->where('request_status', 1); if ($productQuery->count() < 10) { $this->command->warn('Not enough active products found. Seed may use general IDs.'); $products = Product::inRandomOrder()->take(10)->pluck('id')->toArray(); } else { $products = $productQuery->inRandomOrder()->take(10)->pluck('id')->toArray(); } if (empty($products)) { $products = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; } $now = Carbon::now(); // Clear existing to avoid duplicates when re-seeding Schema::disableForeignKeyConstraints(); DB::table('gift_product_assignments')->truncate(); GiftConfiguration::truncate(); Schema::enableForeignKeyConstraints(); $seedData = [ [ 'title' => 'باقة هدايا مميزة', 'slug' => 'special-product-bundle-gift', 'description' => 'احصل على هدايا حصرية من مجموعة منتجاتنا عند الشراء.', 'gift_type' => 'product_wise', 'gift_money_type' => 'flat', 'gift_money_value' => 0.00, 'gift_money_products' => [], 'gift_product_quantity' => 2, 'gift_product_list' => array_slice($products, 0, 3), 'priority' => 1, 'image' => 'def.png', 'start_date' => $now, 'end_date' => $now->copy()->addDays(30), 'status' => 1, ], [ 'title' => 'عرض كاش باك ثابت', 'slug' => 'flat-cashback-offer', 'description' => 'احصل على كاش باك ثابت في محفظتك عند شراء المنتجات المحددة.', 'gift_type' => 'money_wise', 'gift_money_type' => 'flat', 'gift_money_value' => 50.00, 'gift_money_products' => array_slice($products, 3, 3), 'gift_product_quantity' => 0, 'gift_product_list' => [], 'priority' => 2, 'image' => 'def.png', 'start_date' => $now, 'end_date' => $now->copy()->addDays(15), 'status' => 1, ], [ 'title' => 'مكافأة خصم بنسبة مئوية', 'slug' => 'percentage-discount-reward', 'description' => 'استمتع بكاش باك بنسبة 15% في المحفظة على هذه المنتجات الرائعة.', 'gift_type' => 'money_wise', 'gift_money_type' => 'percentage', 'gift_money_value' => 15.00, 'gift_money_products' => array_slice($products, 6, 4), 'gift_product_quantity' => 0, 'gift_product_list' => [], 'priority' => 3, 'image' => 'def.png', 'start_date' => $now, 'end_date' => $now->copy()->addDays(45), 'status' => 1, ], [ 'title' => 'اختيار هدية فاخرة', 'slug' => 'premium-gift-selection', 'description' => 'اختر عنصرًا واحدًا كهدية مجانية عند شرائك لمبلغ معين.', 'gift_type' => 'product_wise', 'gift_money_type' => 'flat', 'gift_money_value' => 0.00, 'gift_money_products' => [], 'gift_product_quantity' => 1, 'gift_product_list' => array_slice($products, 2, 5), 'priority' => 4, 'image' => 'def.png', 'start_date' => $now, 'end_date' => $now->copy()->addDays(60), 'status' => 1, ], [ 'title' => 'باقة الكاش باك الكبرى', 'slug' => 'mega-reward-bundle', 'description' => 'كاش باك ضخم بنسبة 25% على المنتجات المحددة.', 'gift_type' => 'money_wise', 'gift_money_type' => 'percentage', 'gift_money_value' => 25.00, 'gift_money_products' => array_slice($products, 0, 5), 'gift_product_quantity' => 0, 'gift_product_list' => [], 'priority' => 5, 'image' => 'def.png', 'start_date' => $now, 'end_date' => $now->copy()->addDays(20), 'status' => 1, ], ]; foreach ($seedData as $index => $data) { $gift = GiftConfiguration::create($data); // Assign distinct products to avoid a product getting multiple overlapping gifts by default // This ensures a product either has cashback OR product gift. // Using different slices for different configuration index if ($index == 0) { // Product gift $assignProducts = array_slice($products, 0, 2); } elseif ($index == 1) { // Money gift $assignProducts = array_slice($products, 2, 2); } elseif ($index == 2) { // Money gift $assignProducts = array_slice($products, 4, 2); } elseif ($index == 3) { // Product gift $assignProducts = array_slice($products, 6, 2); } else { // Money gift $assignProducts = array_slice($products, 8, 2); } if (!empty($assignProducts)) { $gift->assignedProducts()->sync($assignProducts); } } $this->command->info('GiftConfigurationSeeder seeded successfully. 5 Arabic configurations added.'); } }