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

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

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.

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.