将数据从Vue.js请求传递到Laravel 5.3表单

时间:2016-09-17 23:33:49

标签: laravel-5 vue.js

我有两张桌子:国家和城市。想法是在用户选择国家/地区时显示第二个下拉列表,并将其填入与所选国家/地区对应的城市。

不幸的是我无法弄清楚如何将检索到的值传递给表单。

检索到的值低于国家/地区,ID为21,挪威:)

选择{"国家":" 21"," CountrySelected":true," cities":{" 6&# 34;:" Oslo"," 11":" Lillehammer" }}



addBand(){

  this.form_band.controls.bands.push( 

    this._fb.group({

      name : [ '', Validators.required ],
      score : 0
    })
  );
}

// My app.js

const app = new Vue({


el: '#events',

  data: {
	  	country:'',
	  	CountrySelected: false,	
	  	cities:[], 	
  },

  methods: {

	  	WhenCountryHasBeenSelected: function(event) {
	  		this.getCities();
	  		this.CountrySelected= true;
	  	},

	  	getCities: function(){
	  		this.$http.get('api/cities/'+this.country).then(function (response) {
      this.cities = response.data;
      });
   }
  },

})




1 个答案:

答案 0 :(得分:0)

结束使用Laravel集合地图功能。 Mu路线现在看起来像这样

Route::get('/cities/{country_id}', function (Request $request, $country_id) {
        $collection=App\City::where('country_id', $country_id)->get();
        $cities=$collection->map(function($city){
            return ['id' => $city->id, 'name' => $city->name];
            })->toArray();

Laracast论坛万岁,羞涩:)