# Error in the "Before" and "After" Delete events

**URL:** <https://forum.radsystems.io/t/error-in-the-before-and-after-delete-events/2144>\
**Category:** Bugs\
**Created:** [June 5, 2024, 2:55pm UTC](https://forum.radsystems.io/t/error-in-the-before-and-after-delete-events/2144 "2024-06-05T14:55:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rodrigo](https://forum.radsystems.io/letter_avatar_proxy/v4/letter/r/4af34b/32.png) [@rodrigo](https://forum.radsystems.io/u/rodrigo)\
**Post date:** [June 5, 2024, 2:55pm UTC](https://forum.radsystems.io/t/error-in-the-before-and-after-delete-events/2144/1 "2024-06-05T14:55:23Z")

</div>

When I perform a delete with multiple records (I select more than one), any function I place in “before” or “after” stops working and returns an error. How to resolve?  
If I delete just one record it works normally

---

<div class="post-metadata">

**Author:** ![Wahome-mutahi100](https://forum.radsystems.io/user_avatar/forum.radsystems.io/wahome-mutahi100/32/16_2.png) [@Wahome-mutahi100](https://forum.radsystems.io/u/Wahome-mutahi100)\
**Post date:** [June 5, 2024, 3:51pm UTC](https://forum.radsystems.io/t/error-in-the-before-and-after-delete-events/2144/2 "2024-06-05T15:51:28Z")

</div>

are you trying to create a page event or what do you mean?

---

<div class="post-metadata">

**Author:** ![rodrigo](https://forum.radsystems.io/letter_avatar_proxy/v4/letter/r/4af34b/32.png) [@rodrigo](https://forum.radsystems.io/u/rodrigo)\
**Post date:** [June 5, 2024, 4:18pm UTC](https://forum.radsystems.io/t/error-in-the-before-and-after-delete-events/2144/3 "2024-06-05T16:18:48Z")

</div>

Every time I delete several records it gives me an error and does not execute the event  
“private function afterDelete($rec\_id)”

 ![error0](https://forum.radsystems.io/uploads/default/original/2X/7/702e2378b11874cd237a85cee85d42b9f56d3efa.png)  
 ![error](https://forum.radsystems.io/uploads/default/original/2X/4/4db0a20093ddba5f234eb8753be29e467f6e2f74.png)

---

<div class="post-metadata">

**Author:** ![Wahome-mutahi100](https://forum.radsystems.io/user_avatar/forum.radsystems.io/wahome-mutahi100/32/16_2.png) [@Wahome-mutahi100](https://forum.radsystems.io/u/Wahome-mutahi100)\
**Post date:** [June 5, 2024, 4:32pm UTC](https://forum.radsystems.io/t/error-in-the-before-and-after-delete-events/2144/4 "2024-06-05T16:32:10Z")

</div>

are you using postgres db? however your code is definetly wrong, thats why you are getting the error! explain what you want to do lets see if we can solve it

---

<div class="post-metadata">

**Author:** ![Wahome-mutahi100](https://forum.radsystems.io/user_avatar/forum.radsystems.io/wahome-mutahi100/32/16_2.png) [@Wahome-mutahi100](https://forum.radsystems.io/u/Wahome-mutahi100)\
**Post date:** [June 5, 2024, 4:40pm UTC](https://forum.radsystems.io/t/error-in-the-before-and-after-delete-events/2144/5 "2024-06-05T16:40:13Z")

</div>

try this first

```auto
private function afterDelete($rec_id){
    $tabela = 'operadores';
    $chave = 'id';
    $ID = $rec_id;

    $unix = DB::raw('EXTRACT(epoch FROM deletado::TIMESTAMP)::INTEGER');
    $dados = ['unix_alterou' => $unix];
    DB::table($tabela)->where($chave, $ID)->update($dados);
}

```

---

<div class="post-metadata">

**Author:** ![rodrigo](https://forum.radsystems.io/letter_avatar_proxy/v4/letter/r/4af34b/32.png) [@rodrigo](https://forum.radsystems.io/u/rodrigo)\
**Post date:** [June 5, 2024, 7:20pm UTC](https://forum.radsystems.io/t/error-in-the-before-and-after-delete-events/2144/6 "2024-06-05T19:20:11Z")

</div>

Same error, my code is not wrong.  
Why do I know? Because when I delete just one record, everything goes perfectly. The problem is when I select several records to delete in batch.

---

<div class="post-metadata">

**Author:** ![Wahome-mutahi100](https://forum.radsystems.io/user_avatar/forum.radsystems.io/wahome-mutahi100/32/16_2.png) [@Wahome-mutahi100](https://forum.radsystems.io/u/Wahome-mutahi100)\
**Post date:** [June 6, 2024, 2:35am UTC](https://forum.radsystems.io/t/error-in-the-before-and-after-delete-events/2144/7 "2024-06-06T02:35:35Z")

</div>

oh  
ok, then you need to loop that fist make sure that the array of the deleted records is returned! the issue you have is because the $rec\_id will only be a single id!

---

<div class="post-metadata">

**Author:** ![rodrigo](https://forum.radsystems.io/letter_avatar_proxy/v4/letter/r/4af34b/32.png) [@rodrigo](https://forum.radsystems.io/u/rodrigo)\
**Post date:** [June 6, 2024, 11:28am UTC](https://forum.radsystems.io/t/error-in-the-before-and-after-delete-events/2144/8 "2024-06-06T11:28:36Z")

</div>

I solved the “problem” I saw that a list of IDs was passed, so I just had to transform it into an array and then do a loop

```
/**
 * After record deleted
 * @param string $rec_id // deleted record id
 */
private function afterDelete($rec_id){
    
    $ListaID = explode(",", $rec_id);
    foreach ($ListaID as $d){
    
       /**UPDATE UNIX DELETADO */             
       $Tabela = 'operadores';
       $Chave = 'id';    
       $ID = $d[0];
                            
       $Unix = DB::raw('EXTRACT(epoch FROM deletado::TIMESTAMP(0))::INTEGER');
       $dados = ['unix_alterou'=>$Unix];        
       DB::table($Tabela)->where($Chave, $ID)->update($dados); 
            
       //Atualizar Tabela *operadores_mobile* 
       $row = DB::table('operadores')->where('id',$ID)->first();
       if(!empty($row)){
            $Unix = $row->unix_alterou;
            $Dt_Alterou = $row->dt_alterou;
            $upd = ['unix_alterou'=>$Unix, 'dt_alterou' => $Dt_Alterou, 'deletado'=> $Dt_Alterou,'acesso_mobile'=>'Nao'];
            DB::table('operadores_mobile')->where('id_operador', $ID)->update($upd);
       }
   
    }

}

```

**maybe it will help someone**

---

<div class="post-metadata">

**Author:** ![Wahome-mutahi100](https://forum.radsystems.io/user_avatar/forum.radsystems.io/wahome-mutahi100/32/16_2.png) [@Wahome-mutahi100](https://forum.radsystems.io/u/Wahome-mutahi100)\
**Post date:** [June 6, 2024, 11:41am UTC](https://forum.radsystems.io/t/error-in-the-before-and-after-delete-events/2144/9 "2024-06-06T11:41:48Z")

</div>

that’s well done . good job
