nikname
nikname

Reputation: 161

Laravel saving model without triggering events

I am interested if there is a difference between saving the model using withoutEvents and saveQuietly. What is the main difference between next two pieces of code:

$user = User::withoutEvents(function () use () {
    $user = User::findOrFail(1);
    $user->name = 'Victoria Faith';
    $user->save();
    return $user;
});

and:

$user = User::findOrFail(1);    
$user->name = 'Victoria Faith';    
$user->saveQuietly();

Upvotes: 7

Views: 9599

Answers (1)

jeremykenedy
jeremykenedy

Reputation: 4275

SaveQuietly is a wrapper for the closure WithoutEvents that accepts options:

trait SaveQuietly
{
    /**
     * Save model without triggering observers on model
     */
    public function saveQuietly(array $options = [])
    {
        return static::withoutEvents(function () use ($options) {
            return $this->save($options);
        });
    }
}

Upvotes: 15

Related Questions