3

So i work with Laravel, and i use Laravel excel to load excel/csv files, but my files contains empty rows and i want to delete every empty row.

this is my code :

Excel::selectSheetsByIndex(0)->load($path, function($reader){
       $results = $reader->noHeading()->toArray();
         foreach ($results as $row) {
            //my code
         }
       }, 'UTF-8');

So please if someone has any idea how i can do that i will be very appreciative

Mohammed Hassar
  • 483
  • 1
  • 5
  • 13

2 Answers2

2

I think you can do it in this way

/** @var LaravelExcelReader $data */
        $data = \Excel::load('file.xls', function ($reader) {
            $reader->limitRows(20);
            $reader->ignoreEmpty();
        })->get()->toArray();

        # remove empty rows
        $data = array_filter($data);
Crowlex
  • 101
  • 7
0

use ToCollection method the wrap everything within if($row->filter()->isNotEmpty())

    public function collection(Collection $rows)
       {
          foreach($rows as $row) {
            if($row->filter()->isNotEmpty()){
                // you logic can go here

                $user = User::create([
                    'name' => ucwords($row['name']),
                    'class' => $row['class'],
                    ...
                ]);
            }
        }   
       }