mardi 20 mars 2018

Undefined index error in importing excel

I'm trying to import my excel to my database but the problem is that theres an error saying "Undefined index: title" referring to the title in the controller. So this is my code for my controller

public function importExcel(Request $request)
    {
        if($request->hasFile('import_file')){
            Excel::load($request->file('import_file')->getRealPath(), function ($reader) {
                foreach ($reader->toArray() as $key => $row) {
                    $data['title'] = $row['title'];
                    $data['description'] = $row['description'];

                    if(!empty($data)) {
                        DB::table('items')->insert($data);
                    }
                }
            });
        }

        if(!empty($insert)){
        DB::table('items')->insert($insert);
        dd('Insert Record successfully.');
        }

        return back();
    }   

And this is for my view blade:

@extends('layouts.app')

@section('content')
<div class="container">
    <a href=""><button class="btn btn-success">Download Excel xls</button></a>
    <a href=""><button class="btn btn-success">Download Excel xlsx</button></a>
    <a href=""><button class="btn btn-success">Download CSV</button></a>
    <form style="border: 4px solid #a1a1a1;margin-top: 15px;padding: 10px;" action="" class="form-horizontal" method="post" enctype="multipart/form-data">
        
        <input type="file" name="import_file" />
        <button class="btn btn-primary">Import File</button>
    </form>
</div>
@endsection

Here is the image of the excel i want to upload to my database. It's an xlsx that i want to upload. See picture for reference

But the error says like this Error message screenshot



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2GJxGge
via IFTTT

Aucun commentaire:

Enregistrer un commentaire