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

问题描述 投票:1回答:1

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

我已经需要最新的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

有没有人有什么建议?

php mysql excel laravel laravel-5
1个回答
2
投票

你必须将$fillable变量移动到客户Model。它在错误的文件中,它不能在Controller文件中工作。这就是它缺少字段的原因。模型文件中的$fillable变量可能缺少这些fiels。

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

© www.soinside.com 2019 - 2024. All rights reserved.