Search code examples
phplaravelphpstan

Larastan Call to an undefined method UserModel::merge()


Larastan complain about the following code:

$update = $request->all();

/** @var UserModel $user */
$user = UserModel::where('email', $request->get('email'))->get()->first();

$update['name'] = 'foobar';

$user->merge($update);
$user->save();

Larastan error:

Call to an undefined method UserModel::merge()

PhpStorm also gives a warning:

Method merge() not found

I am not sure what the issue is here


Solution

  • merge() is the method of collection, not Model object.

    After get() method, you got a collection.Applying first() to collection, you get the model object. So it has no method merge();

    if you want to update the fields, you can try update() method:

    $user->update($update);
    

    or set the attribute:

    $user->name = 'foobar';
    $user->save();