| @@ -0,0 +1,42 @@ | |||||
| <?php | |||||
| namespace App\Http\Controllers; | |||||
| use Illuminate\Http\Request; | |||||
| use App\Models\XmlData; | |||||
| class SaveXmlController extends Controller | |||||
| { | |||||
| public function index(Request $req) | |||||
| { | |||||
| if($req->isMethod("POST")){ | |||||
| $xmlDataString = file_get_contents(public_path('places.xml')); | |||||
| $xmlObject = simplexml_load_string($xmlDataString); | |||||
| $json = json_encode($xmlObject); | |||||
| $phpDataArray = json_decode($json, true); | |||||
| if(count($phpDataArray['place']) > 0){ | |||||
| $dataArray = array(); | |||||
| foreach($phpDataArray['place'] as $index => $data){ | |||||
| $dataArray[] = [ | |||||
| "place place_id" => $data['place_id'], | |||||
| "name" => $data['name'], | |||||
| ]; | |||||
| } | |||||
| XmlData::insert($dataArray); | |||||
| return back()->with('success','Data saved successfully!'); | |||||
| } | |||||
| } | |||||
| return view("xml-data"); | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,12 @@ | |||||
| <?php | |||||
| namespace App\Models; | |||||
| use Illuminate\Database\Eloquent\Factories\HasFactory; | |||||
| use Illuminate\Database\Eloquent\Model; | |||||
| class XmlData extends Model | |||||
| { | |||||
| use HasFactory; | |||||
| public $timestamps = false; | |||||
| } | |||||
| @@ -0,0 +1,33 @@ | |||||
| <?php | |||||
| use Illuminate\Database\Migrations\Migration; | |||||
| use Illuminate\Database\Schema\Blueprint; | |||||
| use Illuminate\Support\Facades\Schema; | |||||
| class CreateXmlDataTable extends Migration | |||||
| { | |||||
| /** | |||||
| * Run the migrations. | |||||
| * | |||||
| * @return void | |||||
| */ | |||||
| public function up() | |||||
| { | |||||
| Schema::create('xml_data', function (Blueprint $table) { | |||||
| $table->id(); | |||||
| $table->integer("place_id"); | |||||
| $table->string("name", 250); | |||||
| }); | |||||
| } | |||||
| /** | |||||
| * Reverse the migrations. | |||||
| * | |||||
| * @return void | |||||
| */ | |||||
| public function down() | |||||
| { | |||||
| Schema::dropIfExists('xml_data'); | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,43 @@ | |||||
| <!DOCTYPE html> | |||||
| <html lang="en"> | |||||
| <head> | |||||
| <title>arquitectura</title> | |||||
| <meta charset="utf-8"> | |||||
| <meta name="viewport" content="width=device-width, initial-scale=1"> | |||||
| <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> | |||||
| <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> | |||||
| <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> | |||||
| <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> | |||||
| <style> | |||||
| #frm-create-post label.error{ | |||||
| color:red; | |||||
| } | |||||
| </style> | |||||
| </head> | |||||
| <body> | |||||
| <div class="container" style="margin-top: 50px;"> | |||||
| <h4 style="text-align: center;">Cómo cargar y guardar datos XML</h4> | |||||
| @if ($message = Session::get('success')) | |||||
| <div class="alert alert-success alert-block"> | |||||
| <button type="button" class="close" data-dismiss="alert">×</button> | |||||
| <strong>{{ $message }}</strong> | |||||
| </div> | |||||
| @endif | |||||
| <form action="{{ route('xml-upload') }}" id="frm-create-course" method="post"> | |||||
| @csrf | |||||
| <div class="form-group"> | |||||
| <label for="file">Seleccionar xml:</label> | |||||
| <input type="file" class="form-control" required id="file" name="file"> | |||||
| </div> | |||||
| <button type="submit" class="btn btn-primary" id="submit-post">enviar</button> | |||||
| </form> | |||||
| </div> | |||||
| </body> | |||||
| </html> | |||||
| @@ -2,6 +2,7 @@ | |||||
| use Illuminate\Support\Facades\Route; | use Illuminate\Support\Facades\Route; | ||||
| use App\Http\Controllers\ReadXmlController; | use App\Http\Controllers\ReadXmlController; | ||||
| use App\Http\Controllers\SaveXmlController; | |||||
| /* | /* | ||||
| |-------------------------------------------------------------------------- | |-------------------------------------------------------------------------- | ||||
| @@ -16,6 +17,9 @@ use App\Http\Controllers\ReadXmlController; | |||||
| Route::get("read-xml", [ReadXmlController::class, "index"]); | Route::get("read-xml", [ReadXmlController::class, "index"]); | ||||
| Route::match(["get", "post"], "save-xml", [SaveXmlController::class, "index"])->name('xml-upload'); | |||||
| Route::get('/', function () { | Route::get('/', function () { | ||||
| return view('welcome'); | return view('welcome'); | ||||
| }); | }); | ||||