I am using Laravel-8 and Maatwebsite-3.1 package for Excel Upload:
public function onRow(Row $row)
{
$rowIndex = $row->getIndex();
if($rowIndex >= 1000)
return; // Not more than 1000 rows at a time
$row = $row->toArray();
$employee = Employee::create([
'first_name' => $row[0],
'other_name' => $row[1] ?? '',
'last_name' => $row[2],
'email' => preg_replace('/\s+/', '', strtolower($row[3])),,
'company_id' => Auth::user()->company_id,
'created_at' => date("Y-m-d H:i:s"),
'created_by' => Auth::user()->id,
]);
if (User::where('email', '=', $employee->email)->exists()) {
$user = User::update([
'first_name' => $employee->first_name,
'other_name' => $employee->other_name ?? '',
'last_name' => $employee->last_name,
'updated_at' => date("Y-m-d H:i:s"),
'updated_by' => Auth::user()->id,
]);
}else{
$user = User::create([
'email' => $employee->email,
'username' => strtok($row[3], '@'),
'password' => bcrypt("123456"),
'first_name' => $employee->first_name,
'other_name' => $employee->other_name ?? '',
'last_name' => $employee->last_name,
'created_at' => date("Y-m-d H:i:s"),
'created_by' => Auth::user()->id,
]);
}
}
I got this error:
local.ERROR: ErrorException: Non-static method Illuminate\Database\Eloquent\Model::update() should not be called statically
It points at:
$user = User::update([
How do I resolve this?
Thanks
User::update()is not static. get the user and$user->update(). The error is quite clear I think. How would Laravel know what user to change?