我正在尝试从csv文件上传数据。将数据导入数据库表时,值为NULL。我的CSV文件有价值。
我通过安装Maatwebsite \ Excel来完成此过程。
我的代码
class Template extends Model
{
protected $fillable = ['Origin', 'OriginState', 'Destination' , 'DestinationState' , 'Carrier', 'Car' , 'Buy',
'Carbon', 'DepotFrom', 'DepotTo', 'OriginType', 'DestinationType'
,'GoodsAllowed', 'NonRunners', 'ETA', 'Comments', 'Disabled' ];
}
public function upload(Request $request)
{
if ($request->hasFile('uploadRates')) {
$path = $request->file('uploadRates')->getRealPath();
$data = \Excel::load($path)->get();
if (!empty($data) && $data->count()) {
foreach ($data as $value) {
$arr[] = ['Origin' => $value->Origin,
'OriginState' => $value->OriginState,
'Destination' => $value->Destination,
'DestinationState' => $value->DestinationState,
'Carrier' => $value->Carrier,
'Car' => $value->Car,
'Buy' => $value->Buy,
'Carbon' => $value->Carbon,
'DepotFrom' => $value->DepotFrom,
'DepotTo' => $value->DepotTo,
'OriginType' => $value->OriginType,
'DestinationType' => $value->DestinationType,
'GoodsAllowed' => $value->GoodsAllowed,
'NonRunners' => $value->NonRunners,
'ETA' => $value->ETA,
'Comments' => $value->Comments,
'Disabled' => $value->Disabled,
];
}
if (!empty($arr)) {
DB::table('template')->insert($arr);
return "Success";
}
}
}
dd('Requested file has no Data inside.');
}
和我的CSV数据:
来源,来源州,目的地,目的地状态,载体,汽车,购买,碳,仓库发件人,仓库目的地,来源类型,目的地类型,允许的商品,非跑步者,ETA,注释,已禁用
阿德莱德,SA,墨尔本,VIC,A1汽车 运输车,汽车,250,0,0,0,仓库,仓库,0,0,5,,0 阿德莱德,SA,堪培拉,ACT,A1自动 运输车,汽车609.0909091,0,0,0,仓库,仓库,0,0,7,,0 阿德莱德,SA,悉尼,新南威尔士州,A1汽车 运输车,汽车409.0909091,0,0,0,仓库,仓库,0,0,7,,0 阿德莱德,SA,布里斯班,QLD,A1汽车 运输车,汽车,727.2727273,0,0,0,仓库,仓库,0,0,8,,0 阿德莱德,SA,唐兹维尔,昆士兰州,A1汽车 运输车,汽车,1428.181818,0,0,0,仓库,仓库,0,0,12,,0 阿德莱德,SA,凯恩斯,QLD,A1汽车 运输车,汽车,1491.818182,0,0,0,仓库,仓库,0,0,14,,0
dd($ data)输出:
RowCollection {#787 ▼
#heading: array:17 [▼
0 => "origin"
1 => "originstate"
2 => "destination"
3 => "destinationstate"
4 => "carrier"
5 => "car"
6 => "buy"
7 => "carbon"
8 => "depotfrom"
9 => "depotto"
10 => "origintype"
11 => "destinationtype"
12 => "goodsallowed"
13 => "nonrunners"
14 => "eta"
15 => "comments"
16 => "disabled"
]
#title: "Worksheet"
#items: array:18 [▼
0 => CellCollection {#790 ▼
#title: null
#items: array:17 [▼
"origin" => "Adelaide"
"originstate" => "SA"
"destination" => "Melbourne"
"destinationstate" => "VIC"
"carrier" => "A1 Auto Transporter"
"car" => "Car"
"buy" => 250.0
"carbon" => 0.0
"depotfrom" => 0.0
"depotto" => 0.0
"origintype" => "Depot"
"destinationtype" => "Depot"
"goodsallowed" => 0.0
"nonrunners" => 0.0
"eta" => 5.0
"comments" => null
"disabled" => 0.0
]
}
1 => CellCollection {#792 ▶}
2 => CellCollection {#794 ▶}
3 => CellCollection {#796 ▶}
4 => CellCollection {#798 ▶}
5 => CellCollection {#800 ▶}
6 => CellCollection {#802 ▶}
7 => CellCollection {#804 ▶}
8 => CellCollection {#806 ▶}
9 => CellCollection {#808 ▶}
10 => CellCollection {#810 ▶}
11 => CellCollection {#812 ▶}
12 => CellCollection {#814 ▶}
13 => CellCollection {#816 ▶}
14 => CellCollection {#818 ▶}
15 => CellCollection {#820 ▶}
16 => CellCollection {#822 ▶}
17 => CellCollection {#824 ▶}
]
}
答案 0 :(得分:0)
最可能的原因是,当您将第一行标题用作属性时,它们会显得很慢。
这意味着Origin
将是$value->origin
。
如果要使用原始属性,则可以将excel.php配置中的设置从import.heading
更改为orginal
。
编辑:我刚刚从您的评论中意识到,您的配置中已将startRow设置为2。将其更改为1。
答案 1 :(得分:0)
if (!empty($data) && $data->count()) {
foreach ($data as $value) {
Template::create(
['Origin' => $value->origin,
'OriginState' => $value->origin,
'Destination' => $value->destination,
'DestinationState' => $value->destinationstate,
'Carrier' => $value->$value->carrier,
...
...
]
);
}
}
//不要忘记在模板模型中添加$ fillable属性
protected $fillable = ['Origin', 'Destination', ....]