Matthew
Matthew

Reputation: 1655

Importer function issue using Maat, Excel and Laravel

I know this may seem like a long shot, but I have no idea why my importer is missing some columns and taking in others.

I have required the latest Maat version and even did a test using their example to a table of mine and it worked like a charm with the exact table I am using now.

However, there were a few things I need to happen on the create, so I ended up changing it to what was suggested here: https://laracasts.com/discuss/channels/laravel/how-to-include-created-at-and-updated-at-with-imported-file (about 5th answer down)

So now I have a controller function that looks like this:

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

and this is the top of my controller up until the end of $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'
    ];

I get the following error if I try to post the import:

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

As you see, "created_by" is in fact a field in my MySQL table, by it's not even a field that is being passed or anything like that. Even though you can see it's not from my csv, rather the value comes from $auth::user()->id but there are missing fields that I know for a fact are in my excel sheet (and didn't have a problem being imported when I tried out using the document of just importing everything), and you can see them in my function, and they are fillable so I'm not sure what's going on.

The current missing fields are:

shipping_address_1
shipping_address_2    
shipping_zipcode    
billing_address_1    
billing_address_2    
billing_zipcode    
mobile_phone    
old_account_number    
website
carrier    
notes

Does anyone have any suggestions?

Upvotes: 1

Views: 202

Answers (1)

Laerte
Laerte

Reputation: 7073

You have to move $fillable variable to Customer Model. It is in the wrong file, it is not going to work in the Controller file. This is the reason why it is missing fields. Probably $fillable variable in Model file is missing these fiels.

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

Upvotes: 2

Related Questions