简体   繁体   English

Laravel VueJS Axios将数组发送到控制器

[英]Laravel VueJS Axios send array to controller

I have a laravel 5.4 application which uses VueJS 2.0 for the front end. 我有一个laravel 5.4应用程序,其前端使用VueJS 2.0。

I'm trying to post the visitors array to the controller after populating on the page but when I return the array from the controller the data isn't there. 我试图在页面上填充后将visitor数组发布到控制器,但是当我从控制器返回数组时,数据不存在。

I can't see for the life of me what I'm doing wrong? 我一生都看不到我在做什么错? What's interesting is if I send two visitors through I get two empty arrays returning from my controller so the number of visitors is working it's just not capturing the data. 有趣的是,如果我发送了两个访问者,我从控制器返回了两个空数组,那么访问者的数量正在工作,这只是不捕获数据。

在此处输入图片说明

My vue instance. 我的Vue实例。

  <script>
    new Vue({

    el: '#app',

    data: {
        visitors: [],
        visitor: [],
    },

    methods: {
        addVisitor() {
            this.visitors.push(this.visitor);
            this.visitor = [];
        },

        storeVisit()
        {
          axios({
            method: 'post',
            url: '/visit/new',
            data: {
              visitors: this.visitors
            }
          });
        }
    },

});
  </script>

My HTML page 我的HTML页面

<p>Visitors</p>
    <div class="columns">
      <div class="column">
          <input class="input" placeholder="First Name" v-model="visitor.first">
      </div>
      <div class="column">
          <input class="input" placeholder="Last Name" v-model="visitor.last">
      </div>
      <div class="column">
          <input class="input" placeholder="Company" v-model="visitor.company">
      </div>
      <div class="column">
          <input class="input" placeholder="Email" v-model="visitor.email">
      </div>
      <div class="column">
          <button class="button is-primary" @click="addVisitor()">Add</button>
      </div>
    </div>

I've then got a button that actions the storeVisit() method. 然后,我有一个操作storeVisit()方法的按钮。

<button class="button is-primary" @click="storeVisit">Save Visit</button>

My controller waiting the post request 我的控制器正在等待发布请求

public function store(Request $request)
    {
        return $request->visitors;
    }

My response 我的回复

{"visitors":[[],[]]} { “访客”:[[],[]]}

visitor should be an object, not an array visitor应该是对象,而不是数组

data: {
    visitors: [],
    visitor: {}, // <- object here
},

addVisitor() {
    this.visitors.push(this.visitor);
    this.visitor = {}; // <- reset to an empty object
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM