[Vuejs]-Send array data to laravel controller with Vue

0👍

Your code looks fine, just need to send the array to Laravel. Here’s an example:

submitData(){
  // Here we send the data to '/users/store' route, 
  // using POST method and 'this.users' array as data
  axios.post('/users/store', this.users)
    .then(function (response) {
      // If the request is successful, we can do whatever we want with the response here
      console.log(response);
    })
    .catch(function (error) {
      // If there's an error, treat it here
      console.log(error);
    });
}

Still using the above example, in the UserController you would receive the data like this:

public function store(Request $request)
{
    /*  
        $request->all() contains all the data you sent
        in this case, the users array, but you should always
        validate it before, using FormRequest, for example: 
        https://laravel.com/docs/5.8/validation#form-request-validation
    */
    User::create($request->all()); // Here we save all users to database

    // returning the data you sent before, just for testing.
    return response()->json([$request->all()]);
}

Leave a comment