I am trying to update a sql database table using the data of an excel file (.csv) in Laravel using the Laravel Excel repository.
My Controller function gives back the content of the excel file, but just in an array of 2 (it should be 604).
Therefore, I think I would have to add 'line ending \t' to my function.
But I do not know how.
Here is what I know so far:
The controller
public function uploadExcel()
{
Excel::load(Input::file('import_file'), function ($reader) {
foreach ($reader->toArray() as $value) {
$insert[] = [
'member_title' => $value->member_title,
'member_first_name' => $value->member_first_name,
'member_name_affix' => $value->member_name_affix,
'member_last_name' => $value->member_last_name,
'member_private_address' => $value->member_private_address,
'member_private_zip_code' => $value->member_private_zip_code,
'member_private_location' => $value->member_private_location,
'member_private_phone' => $value->member_private_phone,
'member_private_mobile' => $value->member_private_mobile,
'member_private_fax' => $value->member_private_fax,
'member_private_mail' => $value->member_private_mail,
'member_business_position' => $value->member_business_position,
'member_business_name' => $value->member_business_name,
'member_business_address' => $value->member_business_address,
'member_business_zip_code' => $value->member_business_zip_code,
'member_business_location' => $value->member_business_location,
'member_business_area_code' => $value->member_business_area_code,
'member_business_phone' => $value->member_business_phone,
'member_business_fax' => $value->member_business_fax,
'member_business_mobile' => $value->member_business_mobile,
'member_business_mail' => $value->member_business_mail,
'member_join_date' => $value->member_join_date,
'extra' => $value->extra
];
}
});
if(!empty($insert)) {
die(var_dump($insert)); <-- puts out the array for testing
DB::table('members')->insert($insert);
}
return redirect('index.index');
}
According to the official documentation I would have to add this to my project to recognize the correct line endings:
class UserListImport extends \Maatwebsite\Excel\Files\ExcelFile {
protected $lineEnding = '\t';
}
If my hunch is correct and this snippet from the documentation would solve my problem:
Where do I have to create this file, which contains the code from the documentation?
And do I have to change anything else to make that file take affect?
I am really new to Laravel and I would be very thankful for any kind of help!!
UPDATE
Error message
Class App\UserListImport contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Maatwebsite\Excel\Files\ExcelFile::getFile)
app/UserListImport.php
namespace App;
class UserListImport extends \Maatwebsite\Excel\Files\ExcelFile {
protected $lineEnding = '\t';
public function loadExcel() {
Excel::load(Input::file('import_file'), function ($reader) {
foreach ($reader->toArray() as $value) {
$insert[] = [
'member_title' => $value->member_title,
'member_first_name' => $value->member_first_name,
'member_name_affix' => $value->member_name_affix,
'member_last_name' => $value->member_last_name,
'member_private_address' => $value->member_private_address,
'member_private_zip_code' => $value->member_private_zip_code,
'member_private_location' => $value->member_private_location,
'member_private_phone' => $value->member_private_phone,
'member_private_mobile' => $value->member_private_mobile,
'member_private_fax' => $value->member_private_fax,
'member_private_mail' => $value->member_private_mail,
'member_business_position' => $value->member_business_position,
'member_business_name' => $value->member_business_name,
'member_business_address' => $value->member_business_address,
'member_business_zip_code' => $value->member_business_zip_code,
'member_business_location' => $value->member_business_location,
'member_business_area_code' => $value->member_business_area_code,
'member_business_phone' => $value->member_business_phone,
'member_business_fax' => $value->member_business_fax,
'member_business_mobile' => $value->member_business_mobile,
'member_business_mail' => $value->member_business_mail,
'member_join_date' => $value->member_join_date,
'extra' => $value->extra
];
}
});
if(!empty($insert)) {
die(var_dump($insert));
DB::table('members')->insert($insert);
}
return redirect('index.index');
}
}
Controller
use Maatwebsite\Excel\Facades\Excel;
use App\UserListImport;
public function uploadExcel()
{
UserListImport::loadExcel();
}