Laravel Excel 3.1 抛出 "property doesnt have a default value" 错误尽管导入成功

Laravel Excel 3.1 throws a "property doesnt have a default value" error despite importing successfully

我正在使用 Laravel Excel 包来处理批量上传。虽然我能够成功上传数据,但我的 Web 控制台显示 'staff_id' 没有默认值的错误。我试图将其捕获为异常,但这并没有被触发。我正在使用 ToModel 导入,如下所示

class EmployeesImport implements ToModel, WithHeadingRow
{
    public function model(array $row)
    {
        try {
            return new Employee([
                'staff_id' => $row['staff_id'],
                'first_name' => $row['first_name'],
                'middle_name' => $row['middle_name'],
                'last_name' => $row['last_name'],
                'national_id' => (string) $row['national_id'],
                'department_id' => 1,
            ]);
        } catch (\Exception $e) {
            dd($e->getMessage(), $row);
        }
    }
}

我正在导入的 CSV 具有以下结构

在我的控制器中,我有这个来执行 upload/import

Excel::import(new EmployeesImport(), request()->file('bulk'));

最后,这是我的员工模型,显示了可填写的字段

class Employee extends Model
{
    use SoftDeletes;

    protected $table = "employees";

    protected $fillable = [
        "staff_id", "first_name", "middle_name", "last_name", "national_id", "department_id", "avatar"
    ];
}

(最后一件事)如果它可能具有相关性 - 我的迁移文件的 up 方法

public function up()
{
    Schema::create('employees', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('staff_id')->unique();
        $table->string('first_name');
        $table->string('middle_name')->nullable();
        $table->string('last_name');
        $table->string('national_id')->unique();
        $table->unsignedBigInteger('department_id');
        $table->longText('avatar')->nullable();
        $table->timestamps();
        $table->softDeletes();

        //Foreign keys
        $table->foreign('department_id')->references('id')->on('departments')->onDelete('cascade');
    });
}

根据文档,您可以在最后发现错误

https://docs.laravel-excel.com/3.1/imports/validation.html#gathering-all-failures-at-the-end

Gathering all failures at the end

You can gather all validation failures at the end of the import, when used in conjunction with Batch Inserts. You can try-catch the ValidationException. On this exception you can get all failures.

Each failure is an instance of Maatwebsite\Excel\Validators\Failure. The Failure holds information about which row, which column and what the validation errors are for that cell.

try {
    // import code
} catch (\Maatwebsite\Excel\Validators\ValidationException $e) {
     $failures = $e->failures();

     foreach ($failures as $failure) {
         $failure->row(); // row that went wrong
         $failure->attribute(); // either heading key (if using heading row concern) or column index
         $failure->errors(); // Actual error messages from Laravel validator
         $failure->values(); // The values of the row that has failed.
     }
}

你的模型是这样的:

protected $fillable = [
        "staff_id", "first_name", "middle_name", "last_name", "national_id", "department_id", "avatar"
    ];

你的行是这样的:

return new Employee([
                'staff_id' => $row['staff_id'],
                'first_name' => $row['first_name'],
                'middle_name' => $row['middle_name'],
                'last_name' => $row['last_name'],
                'national_id' => (string) $row['national_id'],
                'department_id' => 1,

只是匹配 $row 和 $fillable,我的意思是在你的 $row 中 "avatar" 必须有一个值来填充 $fillable, 或者您可以从 fillable

中删除 "avatar"