设置列不能为空laravel excel?

问题描述

我正在使用Laravel excel软件包! 我有一个包含一行数据的文件

enter image description here

我的课:

<?PHP

namespace App\Imports;

use App\Models\Product;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\WithheadingRow;

class ProductsImport implements ToModel,WithheadingRow
{
    use Importable;

    public function model(array $row)
    {
        // if (!isset($row[0])) {
        //     return null;
        // }
        return new Product([
            'Trade_name'            => $row['generic_name'],'scientific_name'       => $row['scientific_name'],'company_name'          => $row['company_name'],'desc'                  => $row['description'],'price'                 => $row['price'],'quantity'              => $row['quantity'],'minimum_quantity'      => $row['minimum_quantity_optional'],'expired_date'          => $row['expired_date'],'minimum_expired_date'  => $row['minimum_expired_date_optional'],'provenance'            => $row['provenance_optional'],'variation_id'          => $row['type_optional'],'merchant_id'           => $row['store_id'],'category_id'           => $row['category_id'],'stock'                 => false,]);
    }
}

我遇到错误

sqlSTATE[23000]: Integrity constraint violation: 1048 Column 'Trade_name' cannot be null (sql: insert into `products`

$ row的dd,我得到的数据没有空!

空行在哪里!

Update1:​​

转储$ row

 array(13) { ["generic_name"]=> string(8) "generic1" ["scientific_name"]=> string(11) "Scientific1" ["company_name"]=> string(7) "Company" ["description"]=> string(11) "Description" ["price"]=> int(1000) ["quantity"]=> int(100) ["minimum_quantity_optional"]=> int(100) ["expired_date"]=> int(44137) ["minimum_expired_date_optional"]=> int(44137) ["provenance_optional"]=> string(10) "provenance" ["type_optional"]=> int(1) ["store_id"]=> int(1) ["category_id"]=> int(1) } array(13) { ["generic_name"]=> NULL ["scientific_name"]=> NULL ["company_name"]=> NULL ["description"]=> NULL ["price"]=> NULL ["quantity"]=> NULL ["minimum_quantity_optional"]=> NULL ["expired_date"]=> NULL ["minimum_expired_date_optional"]=> NULL ["provenance_optional"]=> NULL ["type_optional"]=> NULL ["store_id"]=> NULL ["category_id"]=> NULL } 

解决方法

trade_name字段,在迁移或MySQL中使其为空

$table->string('trade_name')->nullable();

并进行电话迁移

php artisan migrate