vendredi 7 juin 2019

How to reflect Laravel Delete Observer for the Model Collection?

I have a TaskObserver class which records the activities.

When I try to delete the task using

::find()

function, it gets reflected in the Task delete observer.

$task = Task::find($id);
$task->delete();

When I try to delete collection using

$task = Task::find($id);
$subTasks = Task::where('parent_id', $task->id)->delete();

It does not reflects the delete observer for any subtasks.

Yes, I searched and found Laravel Event Observer . It tells to use ::find() method so it reflects in the observer.

Do I have to loop through all subtasks and use ::find() method?

$subTasksId = Task::where('parent_id', $task->id)->pluck('id');
foreach($subTasksId as $subTaskId){
    $deleteSubTask = Task::find($subTask)->delete();
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire