php - 如何跳过有关使用 Maatwebsite/Laravel-Excel 导入 excel laravel 的行

标签 php excel laravel laravel-excel

我有一个问题要跳过有关使用 Maatwebsite/Laravel-Excel 包导入 excel laravel 的行。
我从互联网上尝试了很多方法,但仍然没有奏效。

这是我的 Controller 代码

if ($request->hasFile('file')) {
        $import = new HsatuImport();
        $file = $request->file('file'); //GET FILE
        // config(['excel.import.startRow' => 2]);
        Excel::import($import, $file)->limit(false, 2); //IMPORT FILE
        return redirect()->route('admin.hsatus.upload')->withFlashSuccess('Upload Berhasil '.$import->getRowCount().' Data');
    } else {
        return redirect()->route('admin.hsatus.upload')->withFlashSuccess('Upload Gagal');
    }

这是我的导入代码
<?php

namespace App\Imports;

use App\Models\Hsatu;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\ToModel;

class HsatuImport implements ToModel
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    private $rows = 0;
    public function model(array $row)
    {
        ++$this->rows;
        return new Hsatu([
            'region' => @$row[0],
            'nomor_faktur' => @$row[1],
            'nomor_rangka' => @$row[2],
            'kode_mesin' => @$row[3]
        ]);
    }

    public function headingRow(): int
    {
        return 1;
    }

    public function getRowCount(): int
    {
        return $this->rows;
    }
}

最佳答案

当已经存在 Eloquent 模型时,我遇到了跳过的问题。所以在我的代码下面:

public function model(array $row)
    {
        $name = $row['name'];
        if (!Category::where('name', $name)->exists())
            return new Category([
                'name' => $name
            ]);
    }
仅当给定语句为真时,我才返回模型实例。

关于php - 如何跳过有关使用 Maatwebsite/Laravel-Excel 导入 excel laravel 的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60909750/

相关文章:

c# - 获取excel文件的名称

php - 如何从 Laravel 4 中的表单字段获取所有输入文件

php - 添加第二个操作或链接到 Laravel 通知

php - laravel 5 中的 Multi-Tenancy 应用程序?

php $_SERVER ['REQUEST_URI ' ] 编码问题?

php - 我可以设置记录类型和记录类型数组 PHP 吗?

php - 从它的字符串名称中获取一个 PHP 常量

php - 位掩码如何工作?

excel - 在 VBA 代码中压缩多个 OR 条件

vba - 将具有数字值的单元格格式化为数字