使用Maat,Excel和Laravel进口功能问题

时间:2017-12-20 19:26:59

标签: php mysql excel laravel laravel-5

我知道这似乎是一个很长的镜头,但我不知道为什么我的导入器会丢失一些列并接收其他列。

我已经要求使用最新的Maat版本,甚至使用他们的示例进行测试,我的表格就像我现在使用的确切表格一样。

但是,我需要在创建时发生一些事情,所以我最终将其更改为此处建议的内容:https://laracasts.com/discuss/channels/laravel/how-to-include-created-at-and-updated-at-with-imported-file(约第5个答案)

所以现在我有一个看起来像这样的控制器功能:

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

这是我的控制器的最高层,直到$ fillable结束:

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

如果我尝试发布导入,则会出现以下错误:

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

有人有任何建议吗?

1 个答案:

答案 0 :(得分:2)

您必须将$fillable变量移至客户Model。它在错误的文件中,它不能在Controller文件中工作。这就是它缺少字段的原因。

表示模型文件中的$fillable变量可能缺少这些变量。

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