我正在将Laravel-Excel实施到我的项目中,无法确定验证结果。
我尝试上传xlsx
文件,其中包含一行数据,但是导入仍会引发required
错误。
以下是我的EventResultImport
代码
namespace App\Imports;
use App\Models\Data\EventResult;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Imports\HeadingRowFormatter;
class EventResultImport implements ToModel, WithValidation, WithHeadingRow
{
use Importable;
public function model(array $row)
{
HeadingRowFormatter::default('none');
return new EventResult([
'event_id' => $row[0],
'event_name' => $row[1],
]);
}
public function rules(): array
{
return [
//'EventId' => 'required|numeric', Tried this
//'*.EventId' => 'required|numeric', Tried this
'0' => 'required|numeric'
];
}
}
如果列EventId
中有数字数据,则在第二行事件中出现错误。
谢谢
答案 0 :(得分:0)
您正在实现WithHeadingRow,因此属性必须匹配:
public function rules(): array
{
return [
'event_id' => 'required|numeric'
];
}
要跳过空值:
public function model(array $row)
{
if (!isset($row[0])) {
return null;
}
return new User([
'name' => $row[0],
]);
}