0

I want to import some data through Excel file to Database. But while uploading Noting get uploaded in Database.

I'm not sure what's the wrong here, if anyone found out, hope help me to find it.

Here is my controller:

 public function importExcel()
    {
        if(Input::hasFile('import_file')){
            $path = Input::file('import_file')->getRealPath();

            $data = Excel::load($path, function($reader){})->get()->toArray();
            if(!empty($data) && $data->count()){
                foreach ($data as $key => $value) {
                    $insert[] = ['title' => $value->title, 'description' => $value->description];
                }
                if(!empty($insert)){
                    DB::table('items')->insert($insert);
                //  dd('Insert Record successfully.');
                }
            }
        }
        return back();

        }

And here is the blade view:

<html lang="en">
<head>
    <title>Import - Export Laravel 5</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" >
</head>
<body>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <div class="navbar-header">
                <a class="navbar-brand" href="#">Import - Export in Excel and CSV Laravel 5</a>
            </div>
        </div>
    </nav>
    <div class="container">
        <a href="{{ URL::to('downloadExcel/xls') }}"><button class="btn btn-success">Download Excel xls</button></a>
        <a href="{{ URL::to('downloadExcel/xlsx') }}"><button class="btn btn-success">Download Excel xlsx</button></a>
        <a href="{{ URL::to('downloadExcel/csv') }}"><button class="btn btn-success">Download CSV</button></a>
        <form style="border: 4px solid #a1a1a1;margin-top: 15px;padding: 10px;" action="{{ URL::to('importExcel') }}" class="form-horizontal" method="post" >
            <input type="file" name="import_file" />
              {!! Form::token(); !!}
            {!!   csrf_field() ; !!} 
            <button class="btn btn-primary">Import File</button>
        </form>
    </div>
</body>
</html>

And I wanted to upload a file like below Image : enter image description here

2

3 Answers 3

1

please use : use Maatwebsite\Excel\Facades\Excel;

and make sure that name of excel column header is exact same to the database field name :

public function importExcelDemo(Request $request)
{

    $rules = array(
        'import_file' => 'required'
    );

    $validator = Validator::make($request->all(), $rules);
    if ($validator->fails())
    {
        return Redirect::to('/home')->withErrors($validator);
    }
    else
    {
        try
        {
            Excel::load('C:/Users/EBIZ43/Downloads/'. $request['import_file'], function ($reader)
            {
                foreach ($reader->toArray() as $row)
                {
                    $data = $this->ads_repo->prepareData($row);
                    $result = $this->ads_repo->create($data);
                }
            });
            \Session::flash('success', 'Data imported successfully.');
            return redirect('/home');
        }
        catch (\Exception $e)
        {
            \Session::flash('error', $e->getMessage());
            return redirect('/home');
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

I think whats missing is this enctype="multipart/form-data" in your form. Hope it helps

Comments

0

In general, I see you tagged your question Laravel 5 while you are using obsolete Facades.

I would recommend the following updates to your code:

public function importExcel(Request $request)
    {
        $file = $request->file('import_file')
        if($file){
            $path = $file->getRealPath();
            $data = Excel::load($path, function($reader) {
            })->get();
            if(!empty($data) && $data->count()){
                foreach ($data as $key => $value) {
                    $insert[] = ['title' => $value->title, 'description' => $value->description];
                }
                if(!empty($insert)){
                    DB::table('items')->insert($insert);
                //  dd('Insert Record successfully.');
                }
            }
        } 
    }

I would say, you have already installed the Excel library.

1 Comment

I have changed it to your code but nothing changed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.