2017-12-20 42 views
3

Maaatwebsiteを使用して、私のlaravel 5.4.36プロジェクトでエクセルファイルをデータベースにアップロードしようとしましたが、列の検索やテーブルの列構造の逆転がありません。 は、ここに私のcontrolerファイルです:私はエラーを取得し、laravel Maatwebsiteを使用したExcelのimpoirtationで一部の列が見つかりません

<?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(); 
    } 
} 

と私は同じ列ヘッダをテーブルのフィールドです持っているExcelファイルをアップロードしようとすると、これは私のモデル

<?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' 
    ]; 
} 

ある

 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)) 

私は間違っていますか?

+0

「1_30!= 1-30'」ではないですか? –

+0

すべてを1_30または1〜30に変更しても解決しない – Otema

+0

スペースが変更されていることに気付きました_ – Otema

答えて

1

あなたの入力可能な配列変数に似たようにテーブルフィールドを更新し、>フォーム90を削除してください。maatwebsite excelはそれを_に変更するので、MySQLはこれを別のものとして扱います90より大きい場合は、シンボルを使用するのではなく、完全に記述することをお勧めします。最後に、テーブルのフィールド名にスペースがないことを確認してください。

関連する問題