I am using laravel 8 for this project , and I am trying to add elements from an array to the database. On each element I test it's existence etc ... But it is failing to add. I tried to add sleep(3) at the end of each loop , so It added only 3 elements and catches an exception. This is my code :
foreach($request as $value){
if (condition1 {
// post with the same slug already exists
$ide=Universite::select(something)->get();
if(condition2)
{
$etablissement["adresse_id"]=Universite::select(selection2)->get();
$etablissement_=Etablissement::create((array)$etablissement);
$etablissement_->save();
}
}
else {
$adress=Adresse::create((array)$adresse);
$adress->save();
$university["adresse_id"]=$adress->id;
$university=Universite::create((array)$universite);
$university->save();
$etablissement["universite_id"]=$university->id;
$etablissement["adresse_id"]=$adress->id;
$etablissement_=Etablissement::create((array)$etablissement);
$etablissement_->save();
}
if(testing existance)->exists()) {
$specialite["diplome_id"]=Diplome::select(selection)->get()[0]["id"];
}
else {
//creation
$diplome_->save();
$specialite["diplome_id"]=$diplome_->id;
}
if(condition)->exists()){
$mention["domaine_id"]=Domaine::select(selection)->get()[0]["id"];
if(condition)->get()[0]["id"];
if(Scondition) {
//element creation
}
}
else {
//creating things here too
}
}
else {
// creation of other things
}
}
return response()->json('Fichier excel importé avec success!',200);
CodePudding user response: