cafeg/app/Imports/PromoImport.php
2025-01-20 16:42:05 +08:00

37 lines
1.1 KiB
PHP

<?php
namespace App\Imports;
use App\Models\Promocode;
use Illuminate\Support\Collection;
use Log;
use Maatwebsite\Excel\Concerns\ToCollection;
class PromoImport implements ToCollection
{
public function collection(Collection $rows)
{
// 打印每一行數據(這是從 Excel 讀取的行)
foreach ($rows as $row) {
Log::info('Row data: ', ['value' => $row->toArray()[3]]);
if ($row->toArray()[3] != '提货码') // 跳過第一行
{
Promocode::create([
'code' => $row->toArray()[3], // 使用 "提货码" 列
'discount' => 100, // 假設折扣為固定值 100
'type' => 'percent', // 默認為百分比
'usage_limit' => 1, // 默認每個代碼只能使用一次
'valid_from' => date('Y-m-d'), // 當前日期
'valid_to' => date('Y-m-d', strtotime('1 day')), // 默認有效期至第二天
'is_active' => 1, // 默認啟用
]);
}
}
}
}