シェバイクン
シェバイクン

Reputation: 125

Laravel5 event 'deleting' doesn't work on Model::whereIn()

I have one to many relation on the user model, I have set an event when I delete the user will dell all Childs client. on the resource, the Controller destroys method event 'deleting' method is work for normally.

But I create a mass massDestroy method using Model::whereIn() deleting event doesn't work.

Below is my related code, How can I fix it?

UsersController relate code

public function destroy(User $user)
{
    $user->delete();

    return back();
}

public function massDestroy(MassDestroyUserRequest $request)
{
    if ($request->ajax()) {
        User::whereIn('id', $request->get('ids'))->delete();
    }

    return response(null, Response::HTTP_NO_CONTENT);
}

User Model relate code

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use SoftDeletes, Notifiable;

    public $table = 'users';

    //skip

    protected static function boot()
    {
        parent::boot();
        self::deleting(function (User $user) {
           $user->clients()->delete(); //doesn't work on Model::whereIn
        });
    }

    public function clients()
    {
        return $this->hasMany(Client::class, 'user_id', 'id');
    }
}

Client model relate code

public function user()
{
    return $this->belongsTo(User::class ,'user_id', 'id');
}

PS* I have try to delete one by one( very ugly code ) as below is normally work.

UsersController

public function massDestroy(MassDestroyUserRequest $request)
{
    if ($request->ajax()) {
            $users = User::whereIn('id', $request->get('ids'))->get();
            foreach ($users as $user ) {
                $user ->delete();
            }
    }

    return response(null, Response::HTTP_NO_CONTENT);
}

Upvotes: 0

Views: 1330

Answers (3)

Amir Hosseinzadeh
Amir Hosseinzadeh

Reputation: 8458

I have a trick for your problem

It is clearly stated in Laravel documentation Deleting Models that if you need to perform mass operations then none of the events will be fired.

When executing a mass delete statement via Eloquent, the deleting and deleted model events will not be fired for the deleted models. This is because the models are never actually retrieved when executing the delete statement.

My Trick is:

Use this code

$ids = User::whereIn('id', $request->get('ids'))->pluck('id');
User::destroy($ids);

instead of

//User::whereIn('id', $request->get('ids'))->delete();

Upvotes: 0

akshaypjoshi
akshaypjoshi

Reputation: 1295

It is clearly stated in laravel documentation that if you need to perform mass operations then none of the events will be fired. You need to delete them one by one using foreach.

When executing a mass delete statement via Eloquent, the deleting and deleted model events will not be fired for the deleted models. This is because the models are never actually retrieved when executing the delete statement.

Please check note option in Deleting Models

Upvotes: 1

BlackXero
BlackXero

Reputation: 880

You are using function get() to retrieve a collection of records and there is no method delete() on collection, either you delete by using first() function or delete them by looping the collection array.

I hope you got your answer

Upvotes: 1

Related Questions