Я пытаюсь загрузить данные из 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:
Origin, OriginState, Destination, DestinationState, Carrier, Car, Buy, Carbon, DepotFrom, DepotTo, OriginType, DestinationType, GoodsAllowed, NonRunners, ETA, Комментарии, Отключено
Аделаида,SA, Мельбурн, VIC, A1 Auto Transporter, Автомобиль, 250,0,0,0, Депо, Депо, 0,0,5,, 0 Аделаида, SA, Канберра, ACT, A1 Auto Transporter, Автомобиль, 609.0909091,0,0,0, Депо, Депо, 0,0,7,, 0 Аделаида, SA, Сидней, Новый Южный Уэльс, A1 Auto Transporter, Car, 409.0909091,0,0,0, Депо, Депо, 0,0,7,, 0Adelaide, SA, Брисбен, QLD, A1 Auto Transporter, Car, 727.2727273,0,0,0, Депо, Депо, 0,0,8,, 0 Adelaide, SA, Таунсвилл, QLD, A1 Auto Transporter, Car, 1428.181818,0,0,0, Депо, Депо, 0,0,12,, 0 Аделаида, SA, Кэрнс, QLD, A1 Auto Transporter, Car, 1491.818182,0,0,0, Депо, Депо, 0,0,14,, 0
мой снимок базы данных: ![enter image description here](https://i.stack.imgur.com/GMi6g.png)
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 ▶}
]
}