我有2種型號Tour.php
表格模式在laravel與VUE JS結合
public function Itinerary()
{
return $this->hasMany('App\Itinerary', 'tour_id');
}
和Itinerary.php
public function tour()
{
return $this->belongsTo('App\Tour', 'tour_id');
}
tours
表:
id|title|content
itineraries
表:
id|tour_id|day|itinerary
在tour-edit.blade.php
視圖我已經使用vue js爲day
和動態創建或添加和刪除輸入字段。
守則tour-create.blade.php
<div class="row input-margin" id="repeat">
<div class="col-md-12">
<div class="row" v-for="row in rows">
<div class="row">
<div class="col-md-2">
<label >Day:</label>
<input type="text" name="day[]"
class="form-control">
</div>
<div class="col-md-8">
{{ Form::label('itinerary', " Tour itinerary:", ['class' => 'form-label-margin'])}}
{{ Form::textarea('itinerary[]',null, ['class' => 'form-control','id' => 'itinerary']) }}
</div>
<div class="col-md-2">
<button class="btn btn-danger" @click.prevent="deleteOption(row)">
<i class="fa fa-trash"></i></button>
</div>
</div>
</div>
<div class="row">
<button class="btn btn-primary add" @click.prevent="addNewOption" >
<i class="fa fa-plus"></i> Add Field</button>
</div>
</div>
</div>
我想填充與它們各自的數據這些字段。但是,所有屬於旅程的數據(即行程)都將以JSON格式顯示在itinerary
文本框中。
我VUE JS sript是:
<script>
var App = new Vue({
el: '#repeat',
data: {
day:1 ,
rows:[
@foreach ($tour->itinerary as $element)
{day: '{{$element->day}}', plan: '{{$element->plan}}'},
@endforeach
]
},
methods: {
addNewOption: function() {
var self = this;
self.rows.push({"day": "","itinerary":""});
},
deleteOption: function(row) {
var self = this;
self.rows.splice(row,1);
},
}
});
</script>