Jenny Tran
Jenny Tran

Reputation: 33

Could not open for reading! File does not exist Phpexcel with laravel

I have a problem with read file .xls when I using phpexcel-laravel. Im using phpexcel at here https://github.com/Maatwebsite/Laravel-Excel, and I want to read data in file .xls . My .xls have 2 column, column id and column name, I run function getFile and I got message error is "Could not open for reading! File does not exist". I dont know how to fix that. Can you direct me? Thanks you!

My controller:

public function getFile(){
    $results=array();    
    Excel::load('file/test.xls', function($reader) {
        $results = $reader->select(array('id', 'name'))->get()->toArray();
    });

    $data = array(
        'results' = $results;
    );
   return View::make('show_excel',$data)->render();
 }

My view:

@if(!empty($results)) 
    <table class="table table-striped table-bordered">
        <thead>
        <tr>
            <th>ID</th>
            <th>name</th>
        </tr>
        </thead>
        @foreach ($results as $row)
        <tr>
            <td>{{$row->id}}</td>
            <td>{{$row->name}}</td>
        </tr>
        @endforeach 
    </table> 
@endif

Upvotes: 1

Views: 9213

Answers (1)

Ashwini Gupta
Ashwini Gupta

Reputation: 98

public function getFile(){
    $data = Excel::load('file/test.xls', function($reader) {})->get();

    if(!empty($data) && $data->count()){
        foreach ($data->toArray() as $key => $value) {
            $insert[] = ['id' => $value['id'], 'name' => $value['name']];
        }

        if(!empty($insert)){
            Item::insert($insert);
            return back()->with('success','Insert Record successfully.');
        }
    }
}

Use full file path.

Upvotes: 1

Related Questions