0

I'm trying to use Laravel excel https://github.com/Maatwebsite/Laravel-Excel

I have a simple question, how do I put the rows loaded in, into my database, and also how do I display them in a table in my view. The CSV file i'm loading only has one column, with the heading "Email".

Below is my simple code

Controller:

public
function importCSV() {
  $excel = \App::make('excel');
  $excel - > load('kad.csv', function($reader) {

    $reader - > takeColumns(1);

  }) - > get();

  return view('index', compact('excel'));

}

View:

@extends('layouts.master') @section('content')
<table class="table table-striped">
  @foreach($excel as $ex)
  <tr>
    <td>
      {{$ex->Email}}
    </td>
  </tr>
  @endforeach


</table>



@endsection

My view simply displays blank. What is the right way to go about this? Kind regards

MGS
  • 456
  • 1
  • 9
  • 24

1 Answers1

2

Store the result from get() to a variable and pass it to your view.

public function importCSV() {
    $excel = \App::make('excel');
    $data = $excel->load('kad.csv', function($reader) {

        $reader->takeColumns(1);


    })->get();

    return view('index', compact('data'));
}

Then use $data in your blade.

 @foreach($data as $row)
xmhafiz
  • 3,482
  • 1
  • 18
  • 26
  • Lovely, Thanks @xmhafiz. I used a foreach loop in my controller to create instances of the object in my DB and assigned the email attribute from the CSV to the DB objects. – MGS Jul 06 '17 at 05:18