validate([ 'xmlfile' => ['required','mimes:application/xml,xml','max:2048',new Cotationsxml], ]); $is_published = isset($request->is_published) ? true : false; $xmlfile = $request->xmlfile; $path = $xmlfile->store('/cotations/' . date('Y') . '/' . date('m') . '/' . date("d")); //save data to show in pro view $xml = simplexml_load_string(Storage::get($path)); $json = json_encode($xml); $data = json_decode($json,TRUE); $cotations = $data['enregistrement']; //first truncate CotationsData::truncate(); foreach($cotations as $cotation) { $enreg = new CotationsData(); $enreg->produit = $cotation['Produit']; $enreg->qualite = $cotation['Qualité']; $enreg->recolte = $cotation['Récolte']; $enreg->echeance = $cotation['Échéance']; $enreg->prix = isset($cotation['Prix']) ? $cotation['Prix'] : null; $enreg->save(); } $c = new Cotations(); $c->file_path = $path; $c->is_published = $is_published; $c->save(); return redirect()->route('bo.cotations.index'); } }