model = new InnerDevice(); } public function formatDate($date) { if(empty($date)) return ''; return Date::excelToDateTimeObject($date)->format('Y-m-d'); } public function collection(Collection $rows) { if(count($rows) <= 1) { /** @noinspection PhpUnhandledExceptionInspection */ // throw new ImportError('表格为空'); return false; }; foreach ($rows as $key => $row) { if($key == 0) continue; if(!isset($row[0])) break; $device_name = DeviceName::firstOrCreate([ 'name' => $row[1] ]); $spec = Spec::firstOrCreate([ 'device_name_id' => $device_name->id, 'name' => $row[2] ]); $work_point = WorkPoint::firstOrCreate([ 'name' => $row[8] ])->first(); $status = Option::where([ ['table', '=', 'inner_devices'], ['column', '=', 'status'], ['name', '=', $row[9]] ])->first(); $data = [ 'number' => $row[0], 'device_name_id' => $device_name ? $device_name->id : '', 'spec_id' => $spec ? $spec->id : '', 'produce_date' => $this->formatDate($row[3]), 'buy_origin' => $row[4], 'manufacturer' => $row[5], 'shape' => $row[6], 'work_point_id' => $work_point ? $work_point->id : '', 'status' => $status ? $status->id : '', 'quantity' => (int)$row[7] ]; $this->model->create($data); // $num = (int)$row[7]; // for($i = 0; $i < $num; ++$i) { // $this->model->create($data); // } } return true; } }