Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

437
Views
¿Cómo itero esta matriz de objetos en el controlador laravel?

Hago una llamada ajax al hacer clic en el botón y luego envío los datos de la matriz al controlador. Así que esta es la llamada ajax:

 const saveBtnOnClick = () => { //e.preventDefault(); loopMarker(poly); $.ajax({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }, url: "{{ route('storeRouteMap') }}", type: 'POST', data: {markers: markers}, success: function (response) { console.log(response); }, error: function (response) { console.log("Error " + response); } }) }

Y los datos que pasan en las herramientas de desarrollo de carga útil se ven así:

 markers[0][lat]: 3.2845171928345853 markers[0][long]: 101.8885194254942 markers[0][sequence]: 0 markers[0][route_id]: 5 markers[1][lat]: 3.591580368128944 markers[1][long]: 102.5367127848692 markers[1][sequence]: 1 markers[1][route_id]: 5

En el controlador:

 public function store(Request $request): JsonResponse { $out = new ConsoleOutput(); $routeMaps = $request->all(); $out->writeln($routeMaps[1]['long']); try{ foreach($routeMaps as $key=>$value){ $out->writeln($value['lat']); $out->writeln($value['long']); $out->writeln($value['sequence']); $out->writeln($value['route_id']); }

Pero $out->writeln($value['lat']) da error, Undefined array key 'lat' y $out->writeln($routeMaps[1]['long']) da error, Undefined array key 1 ; Entonces, ¿cómo se estructuró esta matriz para que itera?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Está pasando datos en forma de valor clave, es decir, markers: markers . Entonces, su parámetro de solicitud en el controlador sería como: $request->markers . Debe iterar a través $request->markers .

 $routeMaps = $request->markers; foreach($routeMaps as $key => $value){ $out->writeln($value['lat']); $out->writeln($value['long']); $out->writeln($value['sequence']); $out->writeln($value['route_id']); }

Esperemos que esto funcione :)

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!