Problem column 'username' cannot be null
class EmployeeSheetImport implements ToModel, WithHeadingRow, PersistRelations
{
use Importable;
protected $employees;
protected $positions;
protected $roles;
public function __construct()
{
$this->employees = Employee::with('positions', 'user', 'user.roles')->get
();
$this->positions = Position::all()->keyBy('id');
$this->roles = Role::all()->keyBy('id');
}
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
$id = $row['id'];
$username = $row['username'];
$name = $row['name'];
$email = $row['email'];
$password = $row['password'];
$citizen_id = $row['citizen_id'];
// Convert Excel serial date to a PHP Carbon date
$join_date = $this->excelDateToCarbon($row['join_date']);
$birth_date = $this->excelDateToCarbon($row['birth_date']);
$place_of_birth = $row['place_of_birth'];
$gender = $row['gender'];
$marital_status = $row['marital_status'];
$religion = $row['religion'];
$leave_remaining = $row['leave_remaining'] ?? 0;
$role = $row['role'];
$position_id = $row['position_id'];
$roles = [];
if (!empty($role)) {
// Split the comma-separated values into an array
$roles = explode(',', $role);
// Remove any extra whitespace from roles
$roles = array_map('trim', $roles);
}
Leave a Comment
Please, sign-in to leave a comment.