2017-12-20 49 views
1

これは長時間のように思えるかもしれませんが、なぜ私の輸入業者がいくつかの列を見落としていて、Maat、Excel、Laravelを使用したインポート機能の問題

私は最新のMaatバージョンを必要としていて、私のテーブルにその例を使ってテストしても、今使っている正確なテーブルで魅力的に機能しました。

しかし、そこに私が作成で発生する必要がありますいくつかのことだったので、私はここで提案されたものに変更することになった:https://laracasts.com/discuss/channels/laravel/how-to-include-created-at-and-updated-at-with-imported-file

(第5回答ダウンについて)だから今、私は、コントローラの機能を持っています私は次のエラーを取得する

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 
use App\Customer; 
use App\Shipment; 
use App\Equipment; 
use Validator; 
use Response; 
use App\User; 
use Illuminate\Support\Facades\Input; 
use Illuminate\Support\Facades\Auth; 
use DB; 
use Storage; 
use Illuminate\Http\File; 
use Excel; 


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

     protected $rules = 
    [ 
     // 'originName' => 'required|min:2|max:32|regex:/^[a-z ,.\'-]+$/i', 
     'account_type' => 'required' 
    ]; 

     protected $fillable = 
      [ 
     'company_name', 
     'first_name', 
     'last_name', 
     'account_type', 
     'shipping_address_1', 
     'shipping_address_2', 
     'shipping_city', 
     'shipping_state', 
     'shipping_zipcode', 
     'billing_address_1', 
     'billing_address_2', 
     'billing_city', 
     'billing_state', 
     'billing_zipcode', 
      'primary_phone', 
      'mobile_phone', 
      'primary_fax', 
      'old_account_number', 
      'website', 
      'customer_name', 
      'origin', 
      'primary_email', 
      'created_by', 
      'carrier', 
      'notes' 
    ]; 

public function importCustomers (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()){ 
         foreach ($data as $key => $value) { 
          $user = Auth::user()->id; 
          Customer::create([ 
           'company_name' => $value->company_name, 
           'first_name' => $value->first_name, 
           'last_name' => $value->last_name, 
           'account_type' => $value->account_type, 
           'shipping_address_1' => $value->shipping_address_1, 
           'shipping_address_2' => $value->shipping_address_2, 
           'shipping_city' => $value->shipping_city, 
           'shipping_state' => $value->shipping_state, 
           'shipping_zipcode' => $value->shipping_zipcode, 
           'billing_address_1' => $value->billing_address_1, 
           'billing_address_2' => $value->billing_address_2, 
           'billing_city' => $value->billing_city, 
           'billing_state' => $value->billing_state, 
           'billing_zipcode' => $value->billing_zipcode, 
           'primary_phone' => $value->primary_phone, 
           'mobile_phone' => $value->mobile_phone, 
           'primary_fax' => $value->primary_fax, 
           'old_account_number' => $value->old_account_number, 
           'website' => $value->website, 
           'customer_name' => $value->customer_name, 
           'origin' => $value->origin, 
           'primary_email' => $value->primary_email, 
           'created_by' => $user, 
           'carrier' => $value->carrier, 
           'notes' => $value->notes 
          ]); 
         } 
        } 
       } 
     Session::flash('flash_message','Database successfully imported!'); 
     return back(); 
    } 

、これが$充填可能なの最後まで私のコントローラまでのトップである。このようになります私はインポートを投稿しようとした場合:

SQLSTATE[HY000]: General error: 1364 Field 'created_by' doesn't have a default value (SQL: insert into `customers` (`company_name`, `first_name`, `last_name`, `account_type`, `shipping_city`, `shipping_state`, `billing_city`, `billing_state`, `primary_phone`, `primary_fax`, `customer_name`, `origin`, `primary_email`, `updated_at`, `created_at`) values (BULLET LINE, , , 1, OAK CREEK, WI, OAK CREEK, WI, , , BULLET LINE, 0, , 2017-12-20 19:15:47, 2017-12-20 19:15:47)) 

ご覧のように、「CREATED_BY」それも渡されているフィールドまたはそのようなものではありませんで、私のMySQLのテーブルに実際にフィールドです。あなたはそれが私のcsvからではなくても、むしろ値は$auth::user()->idから来ていますが、事実について私が知っている欠落しているフィールドは私のExcelシートにあります(そして、ドキュメントを使って試したときに問題がないすべてをインポートするだけです)、あなたは私の機能でそれらを見ることができます。そして、それらは充足可能なので、何が起こっているのか分かりません。

現在不足しているフィールドは、次のとおりです。

shipping_address_1 
shipping_address_2  
shipping_zipcode  
billing_address_1  
billing_address_2  
billing_zipcode  
mobile_phone  
old_account_number  
website 
carrier  
notes 

誰もが何か提案がありますか?

答えて

3

変数$fillableを顧客Modelに移動する必要があります。これは間違ったファイルにあり、コントローラファイルでは動作しません。これがフィールドを欠いている理由です。おそらくモデルファイルの$fillable変数にこれらのフィールドがありません。

参考:https://laravel.com/docs/5.5/eloquent#mass-assignment

+0

うわは、十分な愚かな過ち笑 – Matthew

+0

問題なしで私を助けるためどうもありがとうございます!私たちは間違いを犯して学ぶ... :) – Laerte