laravel excel get headers and maatwebsite

Marobluns1956 picture Marobluns1956 · Nov 25, 2016 · Viewed 16k times · Source

I want to get only my excel first column that will be column name.

I am using this code

Excel::load($request->file, function ($reader) use($request) {

$torarray=$reader->toArray();
            $line0 = $torarray[0];
            $headers = array_keys($line0);
            $excel_header=$headers;
        });

Sometimes it works but some times not work. when not work with some files the i write below that and works

$torarray=$reader->toArray();
            $line0 = $torarray[0][0];
            $headers = array_keys($line0);
            $excel_header=$headers;
        });

I cant understand whats the correct solution.

Answer

Matija picture Matija · Sep 21, 2017

Thank you @mahmoud-zalt

Just wanted to share how I use it in Laravel 5.5:

$reader = Excel::load(storage_path() . '/uploads/tracking-number/' . $fileName)->get();
$headerRow = $reader->first()->keys()->toArray();

JSON:

["date","reference_no","tracking_notrace_webhttpwww.17track.com_for_eub_and_httpwww.cacesapostal.com_for_swiss_post","destination_country","receiver","tel","state","city","post_code","address","weightkg","quantity","valueusd","description","parcelquantity",0]

var_export:

array (
  0 => 'date',
  1 => 'reference_no',
  2 => 'tracking_notrace_webhttpwww.17track.com_for_eub_and_httpwww.cacesapostal.com_for_swiss_post',
  3 => 'destination_country',
  4 => 'receiver',
  5 => 'tel',
  6 => 'state',
  7 => 'city',
  8 => 'post_code',
  9 => 'address',
  10 => 'weightkg',
  11 => 'quantity',
  12 => 'valueusd',
  13 => 'description',
  14 => 'parcelquantity',
  15 => 0,
)

print_r

Array
(
    [0] => date
    [1] => reference_no
    [2] => tracking_notrace_webhttpwww.17track.com_for_eub_and_httpwww.cacesapostal.com_for_swiss_post
    [3] => destination_country
    [4] => receiver
    [5] => tel
    [6] => state
    [7] => city
    [8] => post_code
    [9] => address
    [10] => weightkg
    [11] => quantity
    [12] => valueusd
    [13] => description
    [14] => parcelquantity
    [15] => 0
)