Laravel - local.ERROR: ErrorException: Non-static method Illuminate\Database\Eloquent\Model::update() should not be called statically

Laravel - local.ERROR: ErrorException: Non-static method Illuminate\Database\Eloquent\Model::update() should not be called statically

我正在使用 Laravel-8 和 Maatwebsite-3.1 包 Excel 上传:

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,
        ]);
     }
}

我收到这个错误:

local.ERROR: ErrorException: Non-static method Illuminate\Database\Eloquent\Model::update() should not be called statically

它指向:

$user = User::update([

我该如何解决?

谢谢

您应该在实例或模型集合上使用 update()。 例如:

$user = User::find($user_id);
$user->update($new_data);

或者如果您想更新所有记录。使用:

User::query()->update($new_data);

试试这个,

$user = User::where('email', '=', $employee->email)->first();
if ($user) {
        $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,
        ]);
     }

为什么不使用 updateOrCreate 方法,像这样的方法对您的场景来说效果很好

$user = User::updateOrCreate(
[   '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
]);

您可以阅读更多相关信息here