Trigger notification on bulk insert

Published 2 months ago by Charrua

Hi, I'm actually saving into the database appointments by inserting them in bulk.

DB::table('goals_appointments')->insert( $appointments_array );

Is there a way to trigger a notification to each appointment inserted?

Best Answer (As Selected By Charrua)
Charrua

@wilburpowery unfortunately ModelName::create($array-of-data) only creates one at a time, remember I need to create a bulk insert. And ModelName::insert($array-of-data) doesn't fire any events:

Model doesn't have an insert method. Calling Model::insert results in a call to Builder::insert through the __call() magic method.

As I need to insert a maximum of 20 bulk records, I ended up by doing:

foreach ($array-of-data as $item){
    $appointment = \App\GoalAppointment::create($item);
    $appointment->notify(new AppNew($appointment));
}
wilburpowery

Yeah.

You can use Eloquent Events. For example, in your model you can have the following:

public static function boot()
{
    parent::boot();
    
    static::created(function ($appointment) {
        event(new AppointmentCreated($appointment));
    })
}

See the docs for Eloquent events here:

https://laravel.com/docs/5.6/eloquent#events

Cronix
Cronix
2 months ago (785,240 XP)

Yes, you'd need to use models. The DB query builder doesn't have "events" (or relationships, or.. or...). It's just a straight up query builder.

Charrua

@wilburpowery great but instead to use an event, can I fire the notification directly from that method?

For example:

public static function boot()
{
    parent::boot();
    
    static::created(function ($appointment) {
        $appointment->notify(new AppIntroNew($appointment));
    })
}

@Cronix I understand that using DB query builder does not fire any model events. I need to create in some way a bulk insert, lets say 100 records... What would be the Eloquent way?

I have found some information on using ModelName::insert($array);, this would be the Eloquent way? and this will trigger the create method that @wilburpowery refers to? Or this will trigger the event and therefore I need to create the listener with the notification fire?

Thank you both.

wilburpowery

Doing ModelName::create($array-of-data) will fire the event I'm pretty sure.

And to use the ->notify method on each appointment, you would need to be using the Notifiable trait.

PatrickL

To bulk insert, you could probably use Database Transactions.


DB::transaction(function() {
    // your insert code here
    
    foreach($appointments as $appointment){
        event(new AppointmentNew($appointment));
    }
}, 5);

Then you can just create the appropriate listener to actually send the notification. I assume you mean a notification sent to the end user when you say "notification".

Also, be sure to make the event queue.

Hope this helps :)

Snapey
Snapey
2 months ago (1,036,705 XP)

insert the records in bulk then separately iterate over the same array sending the emails. No need to do them both in the same operation

Charrua

@wilburpowery unfortunately ModelName::create($array-of-data) only creates one at a time, remember I need to create a bulk insert. And ModelName::insert($array-of-data) doesn't fire any events:

Model doesn't have an insert method. Calling Model::insert results in a call to Builder::insert through the __call() magic method.

As I need to insert a maximum of 20 bulk records, I ended up by doing:

foreach ($array-of-data as $item){
    $appointment = \App\GoalAppointment::create($item);
    $appointment->notify(new AppNew($appointment));
}

Please sign in or create an account to participate in this conversation.