I have one to many relation on user model, I have set a event when I delete user will dell all childs 'client'. on resource Controller destroy 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 relate 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);
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire