laravel Excel 使用 Maatwebsite 导入未找到某些列

laravel Excel impoirtation using Maatwebsite not finding some columns

我正在尝试使用 Maaatwebsite 将 excell 文件上传到我的 laravel 5.4.36 项目中的数据库,但它没有找到某些列并反转 table 列结构。 这是我的控制器文件:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use Maatwebsite\Excel\Facades\Excel;

class DebitController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:admin');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('debit');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        // ...
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // ...
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $debits = DB::table('debits')->get();
        return view('debit')->with(compact('debits'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        // ...
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // ...
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        // ...
    }

    public function import(Request $request)
    {
        if($request->file('imported-file'))
        {
            $path = $request->file('imported-file')->getRealPath();
            $data = Excel::load($path, function($reader) {
            })->get();

            if(!empty($data) && $data->count())
            {
                $data = $data->toArray();
                for($i=0;$i<count($data);$i++)
                {
                $dataImported[] = $data[$i];
                }
            }
            \App\Debit::insert($dataImported);
        }
    return back();
    }
}

这是我的模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Debit extends Model
{
    protected $fillable = [
        'client_name',
        'Current',
        '1_30',
        '31_60',
        '61_90',
        '>90',
        'Total',
        'Total_Pd_chqs',
        'By_30th_Nov',
        'By_5th_Dec',
        'By_15th_Dec',
        'By_20th_Dec',
        'By_31st_Dec',
        'Balance_adjusted',
        'December_Collection_tenants',
        'Status'
    ];
}

当我尝试上传具有相同列 headers 的 excel 文件时,table 字段出现错误

     SQLSTATE[42S22]: Column not found: 1054 Unknown column 'december_collection_tenants' in 'field list' (SQL: insert into `debits` (`1_30`, `31_60`, `61_90`, `balance_adjusted`, `by_15th_dec`, `by_20th_dec`, `by_30th_nov`, `by_31st_dec`, `by_5th_dec`, `client_name`, `current`, `december_collection_tenants`, `status`, `total`, `total_pd_chqs`, `90`) values (-2500, -4500, 5000, -5500, 4500, 2500, -2500, -3000, 5000, Jane anyango, 30000, 500, Ok, 1000, 6500, 3000))

我可能做错了什么?

尝试更新您的 table 字段,使其看起来类似于您的可填充数组变量,然后删除 > 表单 90,因为 maatwebsite excel 会将其更改为 _ 而 MySQL 将也可以将其视为其他东西,如果您的意思是大于 90,则可能是空的 space,那么我建议您将其完整地写下来,而不是使用符号。最后确保您的 table 字段名称

中没有 space