Dhaval Bhavsar
Dhaval Bhavsar

Reputation: 59

In Laravel Observer Not tracking eloquent event like created,updated,deleted

I have on model User in that i am insert record bulk but observer not working in these scenario it will not detect it's create,update,delete event but if i use create it's work perfectly any other work around also accepted. If any one help in these scenario it will be highly appreciate.

Model App/User.php

$raw_data = [
    [
        'name' => 'stackoverflow'
    ],
    [
        'name' => 'stackoverflow1'
    ],

];

User::insert($raw_data);

Upvotes: 1

Views: 1182

Answers (2)

Kenny Horna
Kenny Horna

Reputation: 14241

As @salah said, you can't trigger events when doing mass-operations. As a workaround you could try:

$raw_data = [
    [
        'name' => 'Jane Doe'
    ],
    [
        'name' => 'John Doe'
    ],
];

collect($raw_data)
    ->each(function ($data) {
        User::create($data);
    });

Upvotes: 1

Salah
Salah

Reputation: 877

It's because saved and updated events aren't fired (when bulk updates). Please see the doc:

When issuing a mass update via Eloquent, the saved and updated model events will not be fired for the updated models. This is because the models are never actually retrieved when issuing a mass update.

Nothing you can do. Unless you use a loop to make the creates.

Upvotes: 2

Related Questions