Loading a CSV without a header using Laravel

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
Rontabs
New php-forum User
New php-forum User
Posts: 2
Joined: Sun Jun 09, 2019 7:02 pm

Sun Jun 09, 2019 7:23 pm

Hello All,

This is my first post and I would like to say that I am glad that I have found this place in the internet.

I hope you can help me, so here is my problem:

I am trying to load a CSV that does not have a Header row, meaning that the first row of the CSV is data already, and the identifier for which column it will head into is the column number. I am using Laravel-Excel (Maatwebsite) and the following is my code.

Code: Select all

$path = $request->file('upload')->getRealPath();

$uploads = Excel::load($path)->get();

... 
 
foreach ($uploads as $key => $value) {

	$upload = array();

	$upload = array(
		'DB_Column_01' => $value->$uploads[0],
		'DB_Column_02' => $value->$uploads[1],
		'DB_Column_03' => $value->$uploads[2]
	);

	DB::table('Onboarding_Survey')->insert($upload);
}
The above code does note work as it seems that row 1 is being treated as the header row. Below is the DD output and there is a header key.

Code: Select all

RowCollection {#1643 ▼
  #heading: array:32 [▶]
  #title: "MyFile_2019-05-19"
  #items: array:70 [▶]
}
As per from this link: https://docs.laravel-excel.com/3.1/impo ... g-row.html , which is the documentation, there is a topic on Heading row, where the following is indicated:
In case your file contains a heading row (a row in which each cells indicates the purpose of that column) and you want to use those names as array keys of each row..
...but there is no topic on loading CSV files without a Header row.

Best regards,
Rontabs
Rontabs
New php-forum User
New php-forum User
Posts: 2
Joined: Sun Jun 09, 2019 7:02 pm

Sun Jun 09, 2019 9:00 pm

Ah, I found the solution here:

https://github.com/Maatwebsite/Laravel- ... ssues/1107

Just in case anyone might encounter the same.

Best regards,
Rontabs
Post Reply