1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- <?php
- namespace App\Imports;
- use App\Models\AdminUserArea;
- use App\Models\Area;
- use App\Models\Bike;
- use App\Utils\Admin;
- use Illuminate\Support\Facades\Log;
- use Maatwebsite\Excel\Concerns\OnEachRow;
- use Maatwebsite\Excel\Concerns\ToModel;
- use Maatwebsite\Excel\Row;
- class BikesImport implements OnEachRow
- {
- /**
- * @param array $row
- *
- * @return \Illuminate\Database\Eloquent\Model|null
- */
- public function onRow(Row $row)
- {
- // Log::info($row);
- $admin_id = Admin::user()->id;
- $area = Area::query()->where('admin_id', $admin_id)->first();
- // 投放区域id 在导入excle时 默认为改该管理员第一个创建得区域id
- $put_area_id = 0;
- if(!empty($area)){
- $put_area_id = $area->id;
- }else{
- // 如果是普通管理员 则显示其自己管理的区域id
- $area_id = AdminUserArea::query()->where('admin_id',$admin_id)->first('area_id');
- $put_area_id = $area_id->area_id ?? 0;
- }
- $rowIndex = $row->getIndex();
- $row = $row->toArray();
- return Bike::firstOrCreate([
- 'bike_no' => $row[0],//车辆编号
- // 'box_no' => $row[1],//控制设备
- 'put_area_id' => $put_area_id
- ]);
- }
- }
|