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

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

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

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

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

try this first

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);

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.

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!

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();
            $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

1 Like

that’s well done . good job