gpt4 book ai didi

php - 使用 Maat、Excel 和 Laravel 的导入函数问题

转载 作者:行者123 更新时间:2023-11-29 05:57:40 24 4
gpt4 key购买 nike

我知道这看起来不太可能,但我不知道为什么我的导入程序会丢失一些列并接收其他列。

我需要最新的 Maat 版本,甚至使用他们的示例对我的表格进行了测试,它非常适合我现在使用的表格。

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

所以现在我有一个看起来像这样的 Controller 函数:

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

这是我 Controller 的顶部,直到 $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 表中的一个字段,by 它甚至不是一个正在传递的字段或类似的东西。尽管您可以看到它不是来 self 的 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

有人有什么建议吗?

最佳答案

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

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

关于php - 使用 Maat、Excel 和 Laravel 的导入函数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47913090/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com