Schwesi
Schwesi

Reputation: 4904

Laravel Excel import .csv line endings not recognized

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();
}

Upvotes: 1

Views: 2067

Answers (2)

phaberest
phaberest

Reputation: 3220

The docs assumed you created a new custom class extending Excelfile.

class UserListImport extends \Maatwebsite\Excel\Files\ExcelFile

means exacly this.

I usually prefer to add my own application namespaced folders for anything that my application needs.

It this case I would extract anything out from the controller and call my own class to process Input::file('import_file'). That class is the place where to put that attribute to overwrite how the library interacts with the file.

Upvotes: 1

Andrej
Andrej

Reputation: 7504

This \t means tabulation(gap between words). For line endings the following combinations of chars are used '\n', '\r\n'. It depends on the operating system. '\n' is for Unix/Linux/Mac OS and '\r\n' is for Windows families.

Upvotes: 0

Related Questions