我有一个来自的导入函数laravel excel
,它起作用了,但是现在我想在上面添加一个标题,并且它不起作用,我的excel工作表中只有1列,该标题称为Unit Type
。
<?php
namespace App\Imports;
use App\UnitType;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class UnitTypesImport implements ToCollection, WithHeadingRow
{
protected $user_id;
protected $proj_id;
public function __construct($user_id, $proj_id)
{
$this->user_id = $user_id;
$this->proj_id = $proj_id;
}
public function collection(Collection $rows)
{
foreach ($rows as $row)
{
$unit_types_count = UnitType::where('name', $row[0])->where('project_id', $this->proj_id)->count();
if ($unit_types_count == 0){
UnitType::create([
'name' => $row['unit_type'],
'created_by' => $this->user_id,
'project_id' => $this->proj_id,
]);
}
}
}
public function headingRow(): int
{
return 0;
}
}
当我尝试使用该方法时,WithHeadingRow
它现在给我一个错误:
未定义的偏移量:0
继续@thmspl的答案,
$row['unit_type']
在行中使用
$unit_types_count = UnitType::where('name', $row[0])->where('project_id', $this->proj_id)->count();
代替$row[0]
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句