I am using the Laravel Excel package to handle bulk uploads. Whilst Im able to get the data to upload successfully, my web console indicates and error that 'staff_id' doesn't have a default value. I have tried to catch this as an exception but this does not get triggered. I am using the ToModel import as indicated below
class EmployeesImport implements ToModel, WithHeadingRow
{
public function model(array $row)
{
try {
return new Employee([
'staff_id' => $row['staff_id'],
'first_name' => $row['first_name'],
'middle_name' => $row['middle_name'],
'last_name' => $row['last_name'],
'national_id' => (string) $row['national_id'],
'department_id' => 1,
]);
} catch (\Exception $e) {
dd($e->getMessage(), $row);
}
}
}
The CSV Im importing has the following structure
Within my controller, I have this to exceute the upload/import
Excel::import(new EmployeesImport(), request()->file('bulk'));
And finally, this is my Employees Model, showing the fillable fields
class Employee extends Model
{
use SoftDeletes;
protected $table = "employees";
protected $fillable = [
"staff_id", "first_name", "middle_name", "last_name", "national_id", "department_id", "avatar"
];
}
(One last thing) In case it may hold relevance - my migration file's up method
public function up()
{
Schema::create('employees', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('staff_id')->unique();
$table->string('first_name');
$table->string('middle_name')->nullable();
$table->string('last_name');
$table->string('national_id')->unique();
$table->unsignedBigInteger('department_id');
$table->longText('avatar')->nullable();
$table->timestamps();
$table->softDeletes();
//Foreign keys
$table->foreign('department_id')->references('id')->on('departments')->onDelete('cascade');
});
}