[Vuejs]-Is there any way to pass vuejs object to laravel controller?

3๐Ÿ‘

โœ…

if you need to pass the whole object as a field you have to pass it as a string and then use json_decode on the controller.

Example using axios for the request:

axios.post("your_endpoint",
{your_object_field_name: JSON.stringify(your_object)}).then(res => {
//handle response
})

on the controller:

public function yourControllerMethod(){
   $data = response()->validate('your_object_field_name','required|json'); //validate the field so it is in valid json format
   $jsonArray = json_decode($data); // here you have your json as a php object, pass true as second argument if you want an array (json_decode($data,true))
}
๐Ÿ‘คMario Catillo

0๐Ÿ‘

Below is an example of POST for sending name and email

   //Variables
    data(){
      return{
        object:{
           name: null,
           email: null,
        }
     }
    }

    //In methods
    sendPost(){
       axios.post("http://url-route-exemple.com", this.object).then(result => {
         console.log(result.data); //return laravel
       })
    }

With this you can get the name and email data in the laravel standard (PHP)

//In controller.php
$name = $request->input("name");
$email = $request->input("email");
$jsonNameAndEmail = json_encode($request->all());
๐Ÿ‘คRafael Furtado

Leave a comment