Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

165
Vistas
Got empty array when send array from Axios to Laravel function

I am trying to send multidimensional array from javascript to laravel function and save it in session

my JS array looking like this:

[
   1 => [
       2022-07-12 => [1, 2], [2, 3]
       2022-07-13 => [2, 1], [3, 3]
   ],
   2 => [
       2022-07-14 => [1, 2], [2, 3]
       2022-07-16 => [2, 1], [3, 3]
   ],
]

i have the array in javascript its correct, i am send it to laravel function with axios like that:

Axios.post('/meal-plans/set-weeks-meals', weeksMeals).then((response) => {
   console.log(response.data)
});

and in laravel function i want to save that array in session:

public function setWeeksMeals(Request $request)
{
   session()->put('weeks_meals', $request->weeksMeals);
   return response()->json(['status' => true, 'weeksMeals' => $request->weeksMeals], 200);
}

i got the in my response

{status: true, weeksMeals: null}

and this is my route

Route::post('set-weeks-meals', [Controllers\MealController::class, 'setWeeksMeals'])->name('meals.setWeeksMeals');

when i console.log(weeksMeals);

Array(2)
  1: Array(0)
    2022-07-12: Array(1)
      0: (2) ['1', '1']
      length: 1
      [[Prototype]]: Array(0)
    2022-07-13: Array(1)
      0: (2) ['2', '2']
      length: 1
      [[Prototype]]: Array(0)
    length: 0
    [[Prototype]]: Array(0)
    length: 2
    [[Prototype]]: Array(0)

and in laravel function dd($request->input());

array:2 [
  0 => null
  1 => []
]

and here how i generated the weeksMeals Array

let weeksMeals = [];

if (Array.isArray(weeksMeals[week])) {
    if (Array.isArray(weeksMeals[week][day])) {
        weeksMeals[week][day].push([meal.options[meal.selectedIndex].value, gram.options[gram.selectedIndex].value]);
    } else {
        weeksMeals[week][day] = [];
        weeksMeals[week][day].push([meal.options[meal.selectedIndex].value, gram.options[gram.selectedIndex].value]);
    }
} else {
    weeksMeals[week] = [];
    weeksMeals[week][day] = [];
    weeksMeals[week][day].push([meal.options[meal.selectedIndex].value, gram.options[gram.selectedIndex].value]);
}

can i know how to send this array to laravel function??

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

In order for $request->weeksMeals to work, you need to send the data from your Axios call with weeksMeals as an object index:

Axios.post('/meal-plans/set-weeks-meals', {'weeksMeals': weeksMeals}).then((response) => {
   console.log(response.data)
});

Without that, you were receiving JSON like this:

{'0': [...], '1': [...]}

Which means that $request->weeksMeals is null. $request->input('0') and $request->input('1') would work, or just $request->input(). Once the change is made on the Axios side, the data being sent is:

{'weeksMeals': {'0' => [...], '1' => [...]}}

And $request->weeksMeals would work. I can't speak to the inner arrays not sending properly, that is likely fixed by defining let weeksMeals = {} instead of let weeksMeals = [], since objects accept non-numeric/sequential keys like '1', '2', '2020-01-01', etc, while arrays do not.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda