微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Laravel / Excel中不能导入少于5行

如何解决在Laravel / Excel中不能导入少于5行

我正在尝试将多个行从Excel文件导入数据库。如果行数等于或大于5,则记录会成功添加数据库中,但是如果我尝试的记录少于5,则会出现sql错误

sqlSTATE[23000]: Integrity constraint violation: 1048 Column 'customer_name' cannot be null (sql: insert into `shipments` (`user_id`,`customer_name`,`customer_address`,`customer_email`,`customer_phone`,`destination_country`,`destination_city`,`service_type`,`tb_date`,`tb_time`,`pickup_location`,`product_name`,`quantity`,`consignment_no`,`weights_id`,`product_value`,`product_reference`,`remarks`,`status`,`updated_at`,`created_at`) values (2,?,1220125,1,Booked,2020-10-26 12:26:29,2020-10-26 12:26:29))

这是我的导入课程

<?PHP

namespace App\Imports;

use App\Shipment;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithheadingRow;
Use Auth;
use App\Settings;
use App\ShipmentTracking;
use App\Alert;

class ShipmentImport implements ToCollection,WithheadingRow
{
    public function  __construct($weight_id)
    {
        $this->weight_id= $weight_id;
    }

public function collection(Collection $rows)
{
    $user_id = Auth::User()->id;
    

    foreach ($rows as $row) 
    {
        $consign_no = Settings::where('name','consign_no')->first()->value;
        $increment = $consign_no+1;

        $initial = "12".date("y");
        $consignment_no1 = $initial.$increment;

        $update_setting = Settings::where('name','consign_no')->first();
        $update_setting->value = $increment;
        $update_setting->save();

        $shipment = Shipment::create([
            'user_id'                   => $user_id,'customer_name'             => $row['customer_name'],'customer_address'          => $row['customer_address'],'customer_email'            => $row['customer_email'],'customer_phone'            => $row['customer_phone'],'destination_country'       => $row['destination_country'],'destination_city'          => $row['destination_city'],'service_type'              => $row['service_type'],'tb_date'                   => $row['tb_date'],'tb_time'                   => $row['tb_time'],'pickup_location'           => $row['pickup_address'],'product_name'              => $row['product_name'],'quantity'                  => $row['product_quantity'],'consignment_no'            => $consignment_no1,'weights_id'                => $this->weight_id,'product_value'             => $row['product_value'],'product_reference'         => $row['product_reference'],'remarks'                   => $row['remarks'],'status'                    => 'Booked',]);  
    }
  }
}

我有Laravel版本7。 我使用的Excel文件包是:maatwebsite / excel:3.1

解决方法

这可能是一个老问题,但这可以帮助您

export const getStaticProps = async (context) => {
  // get news here

  return {
    ...
    revalidate: 60 * 60 * 6,}
}

在你的导入类上实现这个

    try {
        Excel::import(new YourImportClass(),request()->file('importar_datos'));
        return back()->with("toast_success","Empleados importados correctamente");
      }catch(\Exception $e)
      {
        return back()->with("toast_error","Line error " . $e->failures()[0]->row() . " on column: " .
         $e->failures()[0]->attribute() . "with message " . $e->failures()[0]->errors()[0] );
      }

还有一个叫做 SkipEmptyrow 的函数 你可以在这里查看文档 https://docs.laravel-excel.com/3.1/imports/validation.html#skipping-empty-rows

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。