2

So I am working on a form where I want o give the user the option of uploading a CSV that will allow the form to be automatically populated. So I thought I would build a function that reads the CSV and then throws each row into an array as an object which I can then pass back to my Laravel Blade template. My only problem is, the array I return from the function is always empty. Any ideas?

private function import($path) {
    $applicants = [];

    Excel::load($path, function(LaravelExcelReader $excel) use ($applicants){
        $excel->each(function(Collection $line) use ($applicants){      
            $name = new \stdClass;    

            $name->first = $line->get('first');
            $name->middle = $line->get('middle');
            $name->last = $line->get('last');
            $name->birthdate = $line->get('birthdate');
            $name->ssn = $line->get('ssn');
            $name->email = $line->get('email');
            $name->mobile_phone = $line->get('mobile_phone');
            $name->home_phone = $line->get('home_phone');
            $name->street = $line->get('street');
            $name->city = $line->get('city');
            $name->state = $line->get('state');
            $name->zip = $line->get('zip');

            array_push($applicants, $name);
        });
    });    

    return $applicants;
}

6 Answers 6

1

Try it by using & operator in the use statement as:

Excel::load($path, function(LaravelExcelReader $excel) use (&$applicants){
   ...
}

OR

Make $applicants a class property then use it in your function as:

private function import($path) {
    $this->applicants = [];

    Excel::load($path, function(LaravelExcelReader $excel) {
        $excel->each(function(Collection $line) {      
            ...

            array_push($this->applicants, $name);
        });
    });    

    return $this->applicants;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Install - https://github.com/Maatwebsite/Laravel-Excel

And then Try below code:

$reader = \Excel::load($file_path);                  //this will load file
$results = $reader->noHeading()->get()->toArray();   //this will convert file to array
foreach($results as $v){
    \\process your array
}

Comments

1

I had problems with using Laravel-Excel library for a very simple csv import, found a better solution using the FastExcel library.

First, install it via composer: composer require rap2hpoutre/fast-excel

And then you can simply use a one liner:

$arrayName = (new \Rap2hpoutre\FastExcel\FastExcel)->import('folder/file.csv')->toArray();

Otherwise:

use Rap2hpoutre\FastExcel\FastExcel;

$arrayName = (new FastExcel)->import('folder/file.csv')->toArray();

It's a quick and easy way to load a csv!

Comments

1

Laravel 6 && Maatwebsite Excel3 Update:Jan 21 2020

$data = Excel::toArray(new \App\Imports\BaseImport(), $request->file('csv_file'));

Base import file

namespace App\Imports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

class BaseImport implements ToCollection {

    /**
     * @param Collection $collection
     */
    public function collection(Collection $rows) {
        return $rows;
    }

}

Comments

0

instead of

array_push($applicants, $name);

try this

$applicants[] = $name;

let me know if it works

Comments

0

A slight refinement to @mujuonly answer, using an anonymous class.

use Maatwebsite\Excel\Concerns\ToCollection;

$array = Excel::toArray(new class implements ToCollection {
        public function collection(\Illuminate\Support\Collection $rows) {
            return $rows;
        }
    }, $csvFile
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.