将excel数据导入laravel时,如何将数据库中的字段sub_category_id与excel数据中的SubCategory进行匹配?

问题描述 投票:0回答:1

我想将excel数据导入laravel数据库,但在我的数据库中,我有sub_category_id字段引用我的sub_categories表,在我的excel模板中,sub_category_id名为SubCategory,我的excel模板中的SubCategory是字符串,但我的sub_category_id是int,怎么搭配呢?

这是我的导入文件:

public function model(array $row)
{
    $qrCode = $this->generateQrCode($row);

    return new FixedAsset([
        'sub_category_id'      => $row[1],
        'specific_location_id' => $row[2],
        'procurement_id'       => $row[3],
        'unit_id'              => $row[4],
        'user_id'              => $row[5],
        'tahun_perolehan'      => $row[6],
        'kode_bmn'             => $row[7],
        'kode_sn'              => $row[8],
        'kondisi'              => $row[9],
        'image'                => $row[10],
        'harga'                => $row[11],
        'keterangan'           => $row[12],
    ]);
}

这是我在控制器中的导入数据功能

public function import(Request $request)
{
    $file = $request->file('file');
    $namaFile = $file->getClientOriginalName();
    $path = public_path('/storage/AssetExcel/' . $namaFile);
    $file->move('storage/AssetExcel', $namaFile);

    try {
        Excel::import(new AssetImport(), $path);

        return redirect()->back()->with(
            'success',
            'Data berhasil diimpor.'
        );
    } catch (\Exception $e) {
        return redirect()->back()->with(
            'error',
            'Terjadi kesalahan saat mengimpor data: ' . $e->getMessage()
        );
    }
}

这是我的Excel模板

没有 子类别 洛卡西 米特拉 萨图安 Pj tahun_perolehan kode_bmn kode_sn 康迪西 图像 哈尔加 科特兰甘
1 宏碁 Nitro 5 TIK XL 单位 贡托·甘塔·纳玛 111111111 SN123456789 tes.png 2000000
excel laravel import laravel-excel
1个回答
0
投票

您可以在创建子类别映射时在导入中构建子类别映射,然后在您的

model
函数中使用它。

protected $subcategories;

public function __construct(...)
{
    $this->subcategories = Subcategory::pluck('id', 'name');
}

public function model(array $row)
{
    $qrCode = $this->generateQrCode($row);

    return new FixedAsset([
        'sub_category_id'      => $this->subcategories->get($row[1]),
        'specific_location_id' => $row[2],
        'procurement_id'       => $row[3],
        'unit_id'              => $row[4],
        'user_id'              => $row[5],
        'tahun_perolehan'      => $row[6],
        'kode_bmn'             => $row[7],
        'kode_sn'              => $row[8],
        'kondisi'              => $row[9],
        'image'                => $row[10],
        'harga'                => $row[11],
        'keterangan'           => $row[12],
    ]);
}

或者对每一行进行查询

public function model(array $row)
{
    $qrCode = $this->generateQrCode($row);

    return new FixedAsset([
        'sub_category_id'      => SubCategory::where('name', $row[1])->first()->id,
        'specific_location_id' => $row[2],
        'procurement_id'       => $row[3],
        'unit_id'              => $row[4],
        'user_id'              => $row[5],
        'tahun_perolehan'      => $row[6],
        'kode_bmn'             => $row[7],
        'kode_sn'              => $row[8],
        'kondisi'              => $row[9],
        'image'                => $row[10],
        'harga'                => $row[11],
        'keterangan'           => $row[12],
    ]);
}
© www.soinside.com 2019 - 2024. All rights reserved.