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

110
Vistas
Javascript objects keep getting converted to array in laravel backend

I started experiencing something strange in my laravel.vuejs application today. When i pass a JS object from vuejs frontend to laravel backend, it gets converted to array. For example, in vuejs

data() {
    return {
        user: {
            name: '',
            email: '',
            age: ''
        },
    }
 },
methods: {
        submit(){
           axios.post(this.api + '/test', {
                user: this.user
            }).then((res) => {
                console.log(res.data)
            })
        }
    },

In my laravel controller, print_r($request->user) shows the object as an array.

Array
(
    [name] => 
    [email] => 
    [age] => 
)

How do i fix this? I am using laravel5.8 NB: I noticed this started happening after i ran composer update. Could this be the cause?

NB: After creating the model, I get back a response of array instead of JSON, even after returning the response as JSON. That is the problem.

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

0

Object concept doesn't exist in HTTP protocol and you can't send your data with requests as an object. you can get the input as json object and decode it, but that will be a StdClass object. So, you can assign the request array to the user model and use it as an object.

Example: Suppose you created a User model and you want to assign the array as an attribute.

$user = new User($request->user);

// or 

$user = new User();
$user->fill($request->user);

// or

$user = new User();
$user->name = $request->user['name']
$user->age = $request->user['age']
...

Note: It's better to validate the inputs before assign to the user object. you can do it clearly with Form Requests

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