【问题标题】:Laravel Excel 3.1 throws a "property doesnt have a default value" error despite importing successfully尽管导入成功,Laravel Excel 3.1 仍会引发“属性没有默认值”错误
【发布时间】:2019-03-21 08:52:27
【问题描述】:

我正在使用 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 文件结构如下

在我的控制器中,我有这个来执行上传/导入

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');
    });
}

【问题讨论】:

  • 关于我看到的文档 Excel::import(new UsersImport, 'users.xlsx');在您的代码中,我看到您将 as new UsersImport() 用作函数,使用其中一个或另一个有什么区别
  • 这应该没问题,在这种情况下两种语法应该以相同的方式工作

标签: laravel-5 laravel-excel


【解决方案1】:

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

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

最后收集所有的失败

您可以收集所有验证 与 Batch 结合使用时,导入结束时失败 插入。您可以尝试捕获 ValidationException。关于这个例外 你可以得到所有的失败。

每个失败都是 Maatwebsite\Excel\Validators\Failure 的一个实例。 失败包含关于哪一行、哪一列和什么的信息 验证错误是针对该单元格的。

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.
     }
}

【讨论】:

  • 我上面定义的 ToModel 的导入定义不会抛出那个异常。
【解决方案2】:

你这样制作模型:

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 中,“头像”必须有一个值来填充 $fillable, 或者您可以从可填充中删除“头像”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多