0

I just installed the last version of Laravel/Excel I followed the tutorial but I get a problem

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'num' cannot be null (SQL: insert into notes (num, cne, nom, prenom, note, matiere, user_id, remarque, sem, ses, an, created_at, updated_at) values (, , , , , , 1, , , , , 2019-05-28 15:36:08, 2019-05-28 15:36:08))

This is my model controller and import class

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Note extends Model
{

    protected $fillable = [
        'num',
        'cne',
        'nom',
        'prenom',
        'note',
        'matiere',
        'user_id',
        'remarque',
        'sem',
        'ses',
        'an',
        'created_at',
        'updated_at'
    ];
}
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Imports\NoteImport;
use Maatwebsite\Excel\Facades\Excel;

class NoteController extends Controller
{

    public function addnote(){
        return view('note.create');
    }

    public function savenote(Request $request){

        Excel::import(new NoteImport, request()->file('file'));

        return redirect()->back()->with('success', 'All good!');
    }

}
<?php

namespace App\Imports;

use App\Note;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use DateTime;

use Auth;
class NoteImport implements ToModel, WithHeadingRow
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        $now = new DateTime();
        return new Note([

            'num'=>$row['num'],
            'cne'=> $row['cne'],
            'nom'=> $row['nom'],
            'prenom'=> $row['prenom'],
            'note'=> $row['note'],
            'matiere'=> $row['matiere'],
            'user_id' => Auth::user()->id,
            'remarque' => $row['remarque'],
            'sem' => $row['sem'],
            'ses' => $row['ses'],
            'an' => $row['an'],
            'created_at'=>$now,
            'updated_at'=>$now,

        ]);
    }
}

This is a picture of the file enter image description here

Mehdi El Aissi
  • 103
  • 1
  • 1
  • 13
  • pretty much explanatory, trying to insert a record and num column is null, most likely reading an empty line as a possible record (all values are empty except for "1", which I'd dare to guess it's a int or a tinyint in your database or the user_id) – abr May 28 '19 at 15:59
  • @abr sorry I don't understand you? I think it's not null on the file as you can see the 1 is from Auth::User()->id – Mehdi El Aissi May 28 '19 at 15:59
  • The error is saying whats wrong and its giving you the value it detects. It's trying to import an empty line of the excel. Check if there are cells with spaces (make sure its blank cells, delete them if required) and try again. https://docs.laravel-excel.com/3.1/imports/model.html – abr May 28 '19 at 16:01
  • @abr I checked everything is not empty all full with data – Mehdi El Aissi May 28 '19 at 16:04

1 Answers1

1

I just added and it's working now! to the noteImport

 if (!isset($row['num'])) {
            return null;
        }
Mehdi El Aissi
  • 103
  • 1
  • 1
  • 13