本文整理汇总了PHP中app\models\Item::firstOrCreate方法的典型用法代码示例。如果您正苦于以下问题:PHP Item::firstOrCreate方法的具体用法?PHP Item::firstOrCreate怎么用?PHP Item::firstOrCreate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\models\Item
的用法示例。
在下文中一共展示了Item::firstOrCreate方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: run
public function run()
{
Model::unguard();
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
$folderpath = base_path() . '/database/seeds/seed_files/';
$folders = File::directories($folderpath);
$latest = '11232015';
foreach ($folders as $value) {
$_dir = explode("/", str_replace('\\', '/', $value));
$cnt = count($_dir);
$name = $_dir[$cnt - 1];
$latest_date = DateTime::createFromFormat('mdY', $latest);
$now = DateTime::createFromFormat('mdY', $name);
if ($now > $latest_date) {
$latest = $name;
}
}
$filePath = $folderpath . $latest . '/Masterfile.xlsx';
$reader = ReaderFactory::create(Type::XLSX);
// for XLSX files
$reader->open($filePath);
// DB::table('divisions')->truncate();
// DB::table('categories')->truncate();
// DB::table('sub_categories')->truncate();
// DB::table('brands')->truncate();
// DB::table('items')->truncate();
foreach ($reader->getSheetIterator() as $sheet) {
if ($sheet->getName() == 'Items') {
$cnt = 0;
Item::where('active', 1)->update(['active' => 0, 'cleared' => 0]);
foreach ($sheet->getRowIterator() as $row) {
if ($row[0] != '') {
if ($cnt > 0) {
$division = Division::firstOrCreate(['division' => strtoupper($row[9])]);
$category = Category::firstOrCreate(['category' => strtoupper($row[1]), 'category_long' => strtoupper($row[0])]);
$sub_category = SubCategory::firstOrCreate(['category_id' => $category->id, 'sub_category' => strtoupper($row[7])]);
$brand = Brand::firstOrCreate(['brand' => strtoupper($row[8])]);
$itemExist = Item::where('sku_code', strtoupper($row[2]))->first();
if (empty($itemExist)) {
$item = Item::firstOrCreate(['sku_code' => trim($row[2]), 'barcode' => $row[3], 'description' => strtoupper($row[4]), 'description_long' => strtoupper($row[5]), 'conversion' => trim($row[6]), 'lpbt' => trim($row[10]), 'division_id' => $division->id, 'category_id' => $category->id, 'sub_category_id' => $sub_category->id, 'brand_id' => $brand->id, 'active' => 1]);
} else {
$itemExist->sku_code = trim($row[2]);
$itemExist->barcode = $row[3];
$itemExist->description = strtoupper($row[4]);
$itemExist->description_long = strtoupper($row[5]);
$itemExist->conversion = trim($row[6]);
$itemExist->lpbt = trim($row[10]);
$itemExist->division_id = $division->id;
$itemExist->category_id = $category->id;
$itemExist->sub_category_id = $sub_category->id;
$itemExist->brand_id = $brand->id;
$itemExist->active = 1;
$itemExist->save();
}
}
$cnt++;
}
}
}
}
$reader->close();
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
Model::reguard();
}