我希望有人能够查看我的代码。
我尝试使用Maat Excel导入记录,但在尝试导入时似乎不想删除几列。
目前,这是我的一行测试来解释我的问题。
bill_to,date,pro_number,sent_on,due_date,shipment_billing_status,ship_to,tractor_id,driver_id,trailer_id,miles,loadDiscountDesc,loadDiscountAmt,fuelChargeDesc,fuelChargeAmt,fuelChargeTotal,permitChargeDesc,permitChargeAmt,permitChargeTotal,otherChargeDesc,otherChargeAmt,otherChargeTotal,Total,balance,shipper_no,load_date,cn_shiptoName,cn_shiptoAddress1,ship_from,created_at,cn_shiptoAddress2,cn_shiptoCity,cn_shiptoState,cn_shiptoZip,cn_shipfromName,cn_shipfromAddress1,cn_shipfromAddress2,cn_shipfromCity,cn_shipfromState,cn_shipfromZip,cn_billtoName,cn_billtoAddress1,cn_billtoAddress2,cn_billtoCity,cn_billtoState,cn_billtoZip,bill_type,print_date,shipment_origin,void,import_balance,import_payments,freightBillSubtotal
157,2017-06-13,213782,,2017-07-13,5,302,,,,,,0,,,,,0,0,,0,,0,0,54451935,2017-06-13,,,,2017-06-13 00:00:00,,,,,fISHER PRICE,501 MEACHAM BLVD,,FORT WORTH,TX,76106,,,,,,,4,,1,,0,0,0
正如您所看到的,特别是一个字段" cn_shipfromName"充满:fISHER价格。
但是如果你看到这个错误:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'cn_shipfromName' cannot be null (SQL: insert into `shipments` (`bill_to`, `date`, `pro_number`, `sent_on`, `due_date`, `shipment_billing_status`, `ship_to`, `tractor_id`, `driver_id`, `trailer_id`, `miles`, `fuelChargeDesc`, `fuelChargeAmt`, `fuelChargeTotal`, `permitChargeDesc`, `permitChargeAmt`, `permitChargeTotal`, `otherChargeDesc`, `otherChargeAmt`, `otherChargeTotal`, `Total`, `shipper_no`, `load_date`, `cn_shiptoName`, `cn_shiptoAddress1`, `cn_shiptoAddress2`, `cn_shiptoCity`, `ship_from`, `created_at`, `cn_shiptoState`, `cn_shiptoZip`, `cn_shipfromName`, `cn_shipfromAddress1`, `cn_shipfromAddress2`, `cn_shipfromCity`, `cn_shipfromState`, `cn_shipfromZip`, `cn_billtoName`, `cn_billtoAddress1`, `cn_billtoAddress2`, `cn_billtoCity`, `cn_billtoState`, `cn_billtoZip`, `bill_type`, `print_date`, `shipment_origin`, `void`, `import_balance`, `import_payments`, `freightBillSubtotal`, `created_by`, `updated_at`) values (157, 2017-06-13, 213782, , 2017-07-13, 5, 302, , , , , , , , , , , , , , , 54451935, 2017-06-13, , , , , , 2017-06-13 00:00:00, , , , , , , , , , , , , , , 4, , 1, , 0, 0, , 1, 2017-12-22 06:22:26))
你会看到fISHER PRICE和其他一些价值观一样。
现在,这是我的控制器:
public function importShipments (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;
Shipment::create([
'bill_to' => $value->bill_to,
'date' => $value->date,
'pro_number' => $value->pro_number,
'sent_on' => $value->sent_on,
'due_date' => $value->due_date,
'shipment_billing_status' => $value->shipment_billing_status,
'ship_to' => $value->ship_to,
'tractor_id' => $value->tractor_id,
'driver_id' => $value->driver_id,
'trailer_id' => $value->trailer_id,
'miles' => $value->miles,
'loadDiscountDesc' => $value->loadDiscountDesc,
'loadDiscountAmt' => $value->loadDiscountAmt,
'fuelChargeDesc' => $value->fuelChargeDesc,
'fuelChargeAmt' => $value->fuelChargeAmt,
'fuelChargeTotal' =>$value->fuelChargeTotal,
'permitChargeDesc' => $value->permitChargeDesc,
'permitChargeAmt' => $value->permitChargeAmt,
'permitChargeTotal' => $value->permitChargeTotal,
'otherChargeDesc' => $value->otherChargeDesc,
'otherChargeAmt' => $value->otherChargeAmt,
'otherChargeTotal' => $value->otherChargeTotal,
'Total' => $value->Total,
'balance' =>$value->balance,
'shipper_no' => $value->shipper_no,
'load_date' => $value->load_date,
'cn_shiptoName' => $value->cn_shiptoName,
'cn_shiptoAddress1' => $value->cn_shiptoAddress1,
'cn_shiptoAddress2' => $value->cn_shiptoAddress2,
'cn_shiptoCity' => $value->cn_shiptoCity,
'ship_from' => $value->ship_from,
'created_at' =>$value->created_at,
'cn_shiptoState' => $value->cn_shiptoState,
'cn_shiptoZip' => $value->cn_shiptoZip,
'cn_shipfromName' => $value->cn_shipfromName,
'cn_shipfromAddress1' => $value->cn_shipfromAddress1,
'cn_shipfromAddress2' => $value->cn_shipfromAddress2,
'cn_shipfromCity' =>$value->cn_shipfromCity,
'cn_shipfromState' =>$value->cn_shipfromState,
'cn_shipfromZip' => $value->cn_shipfromZip,
'cn_billtoName' => $value->cn_billtoName,
'cn_billtoAddress1' => $value->cn_billtoAddress1,
'cn_billtoAddress2' => $value->cn_billtoAddress2,
'cn_billtoCity' => $value->cn_billtoCity,
'cn_billtoState' => $value->cn_billtoState,
'cn_billtoZip' => $value->cn_billtoZip,
'bill_type' =>$value->bill_type,
'print_date' => $value->print_date,
'shipment_origin' => $value->shipment_origin,
'void' => $value->void,
'import_balance' => $value->import_balance,
'import_payments' => $value->import_payments,
'freightBillSubtotal' => $value->freightBillSubtotal,
'created_at' => $value->created_at,
'created_by' => $user,
]);
}
}
}
Session::flash('flash_message','Database successfully imported!');
return back();
}
这是我的SHIPMENT模型,其中包含特定于此问题的部分:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Shipment_Status;
use App\Shipment_Billing_Status;
use App\Shipment_Type;
use OwenIt\Auditing\Contracts\Auditable;
class Shipment extends Model implements Auditable
{
use \OwenIt\Auditing\Auditable;
protected $fillable = [
'pro_number',
'shipment_origin',
'notes',
'shipment_status',
'shipment_billing_status',
'due_date',
'date',
'description',
'driver_id',
'tractor_id',
'trailer_id',
'shipper_no',
'ship_to',
'ship_from',
'bill_to',
'bill_type',
'load_date',
'cn_billtoName',
'cn_billtoAddress1',
'cn_billtoAddress2',
'cn_billtoCity',
'cn_billtoState',
'cn_billtoZip',
'cn_billtoPhone',
'cn_billtoEmail',
'cn_shiptoName',
'cn_shiptoAddress1',
'cn_shiptoAddress2',
'cn_shiptoCity',
'cn_shiptoState',
'cn_shiptoZip',
'cn_shiptoPhone',
'cn_shiptoEmail',
'cn_shipfromName',
'cn_shipfromAddress1',
'cn_shipfromAddress2',
'cn_shipfromCity',
'cn_shipfromState',
'cn_shipfromZip',
'cn_shipfromPhone',
'cn_shipfromEmail',
'fuelChargeDesc',
'fuelChargeAmt',
'fuelChargeTotal',
'permitChargeDesc',
'permitChargeAmt',
'permitChargeTotal',
'otherChargeDesc',
'otherChargeAmt',
'otherChargeTotal',
'noCharge',
'noSettle',
'Total',
'freightBillSubtotal',
'sent_on',
'created_at',
'print_date',
'void',
'import_payments',
'import_balance',
'miles',
'sent_on',
'created_by'
];
答案 0 :(得分:0)
如果允许某些列为cn_shipfromName
的列为空,则必须向迁移中添加->nullable()
$table->string('cn_shipfromName')->nullable();
并重新运行迁移:
php artisan migrate:refresh --path=/path/to/the/migrationfile
如果不允许空值,则验证应将其标记为无效并将其从导入中排除。
答案 1 :(得分:0)
您可以将$value
转换为数组,然后使用它填充模型。
$input = $value->toArray();
$shipment = new Shipment;
$shipment->fill($value);
$shipment->save();
然后,为了调查导致问题的原因,您可以转储您的货件对象,然后在保存之前检查其中分配了哪些值:
$shipment->fill($value);
dd($shipment);
$shipment->save();