1

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

4
  • What error did you get? Commented Sep 3, 2021 at 10:20
  • @Collin - Sorry. I have updated my code. This is the error: local.ERROR: ErrorException: Non-static method Illuminate\Database\Eloquent\Model::update() should not be called statically Commented Sep 3, 2021 at 10:26
  • 2
    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? Commented Sep 3, 2021 at 10:26
  • @GertB. - How do you mean Commented Sep 3, 2021 at 10:27

3 Answers 3

0

try this,

$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,
        ]);
     }
Sign up to request clarification or add additional context in comments.

Comments

0

You should use update() on an instance or collection of models. Eg:

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

Or if you want to update all records. Use:

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

Comments

0

why not use the updateOrCreate method, something like this will work just fine for your scenario

$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
]);

You can read more about it here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.