Ho Huu Khanh
Ho Huu Khanh

Reputation: 13

Undefined index: code use Export to Excel in Laravel 5.8

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    [email protected] Male 4/9/1998   USA 1 
B7777   Tom         Cruise      tomcruise   123456    123456    [email protected] Male    4/5/1998 Canada 1 
B6666   Lena        Do          lenado      123456    123456    [email protected]    Male    9/4/1997    USA 2

Upvotes: 0

Views: 2404

Answers (2)

Muhammad Shoaib
Muhammad Shoaib

Reputation: 21

Add this line in your AlumniImport class:

use Maatwebsite\Excel\Concerns\WithHeadingRow;

After this, include WithHeadingRow in your AlumniImport class like this:

class AlumniImport implements ToModel,WithHeadingRow

Upvotes: 0

N69S
N69S

Reputation: 17216

You can confirm my suggestion by doing a var_dump($row);die();

what i found on the package Maatwebsite, the $row has numeric indexes.

try this

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

-----edit-----

dont forget to put the fields in the $fillable of the User::class

class User extend Model
{
    protected $fillable = ['code','first_name', 'last_name', 'username', 'password', 'tel', 'email', 'gender', 'birthday', 'address', 'status_id'];
    .....
}

if you dont wanna put these fields as fillable and i dont recommend you to do it (especially for the password field) you can do it this way;

class AlumniImport implements ToModel
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        $user = new User();
        $user->code = $row[0];
        $user->first_name = $row[1];
        $user->last_name = $row[2];
        $user->username = $row[3];
        $user->password = Hash::make($row[4]);
        $user->tel = $row[5];
        $user->email = $row[6];
        $user->gender = $row[7];
        $user->birthday = $row[8];
        $user->address = $row[9];
        $user->status_id = $row[10];
        return $user;
    }
}

Upvotes: 2

Related Questions