程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Maatwebsite Excel 中的未定义索引大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Maatwebsite Excel 中的未定义索引?

开发过程中遇到Maatwebsite Excel 中的未定义索引的问题如何解决?下面主要结合日常开发的经验,给出你关于Maatwebsite Excel 中的未定义索引的解决方法建议,希望对你解决Maatwebsite Excel 中的未定义索引有所启发或帮助;

我正在尝试在 Laravel 中导入 Excel 数据并插入到数据库中。我正在使用 maatwebsite/excel 版本 3 composer 包。

错误:未定义索引:CUSTOMER_NAME

刀片文件:import_excel.blade.php

<form method="post" enctype="multipart/form-data" action="{{ url('/import_excel/import') }}">
    {{ csrf_fIEld() }}
    <div class="form-group">
        <table class="table">
            <tr>
                <td wIDth="40%" align="right"><label>SELEct file for Upload</label></td>
                <td wIDth="30">
                    <@R_301_5983@ type="file" name="SELEct_file" />
                </td>
                <td wIDth="30%" align="left">
                    <@R_301_5983@ type="submit" name="upload" class="btn btn-priMary" value="Upload">
                </td>
            </tr>
            <tr>
                <td wIDth="40%" align="right"></td>
                <td wIDth="30"><span class="text-muted">.xls,.xslx</span></td>
                <td wIDth="30%" align="left"></td>
            </tr>
        </table>
    </div>
</form>

导入文件:Customerimport.php

public function model(array $row)
{
    $data = [];

    foreach ($row as $value)
    {
        $data[] = array(
            'Customername' => $row['CUSTOMER_NAME'],'gender' => $row['gender'],'Address' => $row['address'],'City' => $row['city'],'PostalCode' => $row['postal_cole'],'Country' => $row['country']
        );
    }
    
    DB::table('customers')->insert($data);
}

控制器功能

public function import(request $request)
{
    $this->valIDate($request,[
        'SELEct_file' => 'required|mimes:xls,xLSX'
    ]);

    $path = $request->file('SELEct_file')->getRealPath();

    Excel::import(new Customerimport,$path);
    return BACk()->with('success','Excel Data imported successfully.');
}

Excel 图像

Maatwebsite Excel 中的未定义索引

有人可以指导我吗?

解决方法

要使用 heading row,您需要像这个例子一样实现 WithHeadingRow

namespace App\Imports;

use App\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class UsersImport implements ToModel,WithHeadingRow
{
    public function model(array $row)
    {
        return new User([
            'name'  => $row['name'],'email' => $row['email'],'at'    => $row['at_field'],]);
    }
}

大佬总结

以上是大佬教程为你收集整理的Maatwebsite Excel 中的未定义索引全部内容,希望文章能够帮你解决Maatwebsite Excel 中的未定义索引所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。