如何在Laravel中存储对象数组

时间:2020-06-30 16:28:12

标签: php laravel vue.js

我有这张表格

import AppForm from '../app-components/Form/AppForm';
Vue.component('shipping-rules-form', {
    mixins: [AppForm],
    props:['cities','countries'],
    data: function() {
        return {
                inputs: [
                    {
                        lower_bound:'',
                        upper_bound:'',
                        price:'',
                    }
                ],
  
        };
    },
    methods: {
        addInput(){
            this.form.inputs.push({lower_bound:'',upper_bound:'',price:''});
        },
        removeInput(index){
            this.form.inputs.splice(index, 1);
        },
    },
    computed:{
        elements()
        {
            if(this.form.shippable_type == 1) {
                return this.countries;
            } else {
                return this.cities;
            }
        },

    }

});

和我的.js文件都有这个

$json = $request->input('inputs');

我想将输入(对象数组)存储为json

在我的控制器中,我已经完成了

[ 
   {
     "id": 1,
     "name": "Karate",
   }, 
   {
      "id": 2,
      "name": "Paintball",
   },
   {
      "id": 3,
      "name": "Rugby",
   },
   {
      "id": 4,
      "name": "Squash",
   },
   {
      "id": 5,
      "name": "Softball",
   },
   {
      "id": 6,
      "name": "Swimiming",
   },
   {
      "id": 7,
      "name": "Weighlifting",
   },
   {
      "id": 8,
      "name": "Table Tennis",
   },
   {
      "id": 9,
      "name": "Tenpin Bowling",
   }
]

但是$ json返回null。

我只能正确返回所有其他数据,这只是返回null。

我也尝试过纯粹使用html和laravel而不使用vue组件,但它返回的是null。

1 个答案:

答案 0 :(得分:0)

提交表单时,您可以将命名值传递为lower_bound和upper_bound,但是在控制器中,您可以访问输入,这就是$ json变量显示空值的原因。您可以使用name作为数组名=“ lower_bound []”,与upper_bound相同,并在控制器中通过索引号$ json = $ request-> lower_bound [0];

来访问值。