php - How to delete this items in laravel5 (array)? -


i tried delete items received correctly in foreach :

 public function prueba(){         $detallcomanda = comanda::where('idusuari','=',14)->where('estat','=',0)->get();         if(count($detallcomanda)>0){             foreach($detallcomanda $detall){                  echo $detall->id;             }         }     } 

i show id correctly when try delete form in foreach laravel returns.

whoops, looks went wrong.

$detall->delete($detall->id) 

update

when try recieve content correctly , when use comanda::destroy laravel5 don't doing anything.

 public function prueba(){         $comanda = comanda::where('idusuari','=',1)->where('estat','=',0)->get();         $todelete = array();         foreach($comanda $detall){             $todelete[] = $detall->id;         }         var_dump($todelete);      } 

this array

array(5) { [0]=> int(41) [1]=> int(42) [2]=> int(43) [3]=> int(44) [4]=> int(45) } 

and when use following code , destroy doesn't work.

comanda::destroy($todelte)

update2

i doing

$prueba = json_encode($todelete); var_dump($prueba); 

and recieve id correctly , need use explode quit items "[ ]" , numbers don't know how works explode in laravel or same?

string(7) "[38,41]" comanda::destroy([10, 14]); 

you don't want delete them individually in loop that. means you're running 1 query per item deleted. instead gather ids in array , delete them in 1 query:

public function prueba(){     $detallcomanda = comanda::where('idusuari','=',14)->where('estat','=',0)->get();     $todelete = array();     if(count($detallcomanda)>0){         foreach($detallcomanda $detall){              $todelete[] = $detall->id;         }         comanda::destroy($todelete);     } } 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

Kivy: Swiping (Carousel & ScreenManager) -

jdbc - Not able to establish database connection in eclipse -