Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88886

Undefined index: code use Export to Excel in Laravel 5.8

$
0
0

I'm executing Export, Import to Excel in Laravel. But I have an error

Undefined index: code

in a file AlumniImport.php. Thank you for help!

AlumniImport.php

namespace App\Imports;

use App\Models\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Illuminate\Support\Facades\Hash;

class AlumniImport implements ToModel
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        return new User([
            'code'          => $row["code"],
            'first_name'    => $row["first_name"],
            'last_name'     => $row["last_name"],
            'username'      => $row["username"],
            'password'      => Hash::make($row["password"]),
            'tel'           => $row["tel"],
            'email'         => $row["email"],
            'gender'        => $row["gender"],
            'birthday'      => $row["birthday"],
            'address'       => $row["address"],
            'status_id'     => $row["status_id"],
        ]);
    }
}

AlumniController.php

// Excel
use App\Imports\AlumniImport;
use App\Exports\AlumniExport;
use Excel;
class AlumniController extends Controller
{
 public function import()
    {
        Excel::import(new AlumniImport,request()->file('file'));
        return back();  
    }
}

Example of data in Excel:

code    first_name  last_name   username    password    tel     email      gender birthday  address status_id
B8888   John        Smith       johnsmith   123456    123456    johnsmith@gmail.com Male 4/9/1998   USA 1 
B7777   Tom         Cruise      tomcruise   123456    123456    tomcruies@gmail.com Male    4/5/1998 Canada 1 
B6666   Lena        Do          lenado      123456    123456    lenado@gmail.com    Male    9/4/1997    USA 2

Viewing all articles
Browse latest Browse all 88886


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>