Laravel get contents of Excel to insert in Database

Laravel get contents of Excel to insert in Database

I'm having a problem while uploading excel file and update data to my database using Laravel 5.6 and Laravel Excel(maatwebsite 3.1) i can't get the contents of each column to update the data in my database.

I'm having a problem while uploading excel file and update data to my database using Laravel 5.6 and Laravel Excel(maatwebsite 3.1) i can't get the contents of each column to update the data in my database.

$rows return empty

Model:

namespace App;

use App\User; use App\MyModel; use Illuminate\Support\Collection; use Maatwebsite\Excel\Concerns\ToCollection;

class DataImport implements ToCollection { /** * @param array $rows * * @return \Illuminate\Database\Eloquent\Model|null */ public function collection(Collection $rows) { dd($rows); foreach ($rows as $row) { $import = MyModel::where('user_id',$row[1])->first(); $import->field1= $row[2]; $import->field2= $row[5]; $import->field3= $row[3]; $import->status = 'PAID'; $import->save(); }

}

}

Controller:

use Maatwebsite\Excel\Excel;

public function postImport(Request $request){
    if($request->hasFile('sample_file')){
        Excel::import(new DataImport ,request()->file('sample_file'));

        return redirect('/import');
    }

}

Excel File:

--------------------------------------------------------------------------------
| #  | USER ID | FIELD ONE | FIELD TWO | FIELD THREE | FIELD FOUR | FIELD FIVE |


| 1 | 1 | 123-abc | abc-123 | 123123 | asdf | konjesh |


php excel laravel

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Php how to delete multiple rows through checkbox using ajax in laravel

In this article i will let you know to delete multiple rows through checkbox using ajax in laravel and before delete we will give a confirmation message.

Laravel Export in Excel using maatwebsite/excel

This tutorial is on "maatwebsite/excel" package that is built on top of the "phpoffice/phpspreadsheet". In this video, you will learn how to export large dat...

5 Laravel’s Hidden Gems

Spread the love1. Stop on first validation error By default, Laravel will check for all validation rules and return a list of errors. But if you want to stop this process after first validation failure, that’s how you can achieve…Read More→

Some of the most frequent how tos in Laravel

Spread the loveHow to get relationship from relationship using With() in Laravel Some times there are cases where you want to get relationship from relationship in Laravel, that can be achieved via following: How to create multiple where clauses in…Read More→

Laravel Excel Import to Database

In this tutorial, you will learn how to import large-sized excel into a database using the Laravel Excel package. This Laravel excel package provides the fea...