从动态创建的输入字段将数据更新到表中 [英] Update data into table from dynamically created input field

查看:55
本文介绍了从动态创建的输入字段将数据更新到表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个模型 Tour.php

public function Itinerary()
{
    return $this->hasMany('App\Itinerary', 'tour_id');
}

Itinerary.php

public function tour() 
{
    return $this->belongsTo('App\Tour', 'tour_id');
}

游览表:

id|title|content

入门表:

id|tour_id|day|itinerary

我已经使用vue js创建或添加和删除了一天的输入字段,并进行了动态计划.并在tour.store方法中使用以下代码插入到itineraries表中:

I have used vue js to create or add and remove input field for day and plan dynamically. And used the following code in tour.store method to insert into itineraries table:

    $count = count($request->input('day'));
$temp_day        = $request->input('day');
$temp_itinerary  =   $request->input('itinerary');

for($i = 0; $i < $count; ++$i) 
{

    $itinerary = new Itinerary;
    $itinerary->tour_id        =  $tour->id;
    $itinerary->plan      =   $temp_itinerary[$i];        
    $itinerary->day            =   $temp_day[$i];
    $itinerary->save();
}

成功插入记录.并在tour.store方法中应用了相同的代码.它没有更新行,而是向表中插入了新行.最好的解决方案是什么?

And was successful in inserting the records.And applied same code in tour.store method. Instead of updating the rows, it inserted new rows to the table. What would be the best solution for this ?

推荐答案

要进行更新,请尝试以下代码

For updation try this code

$itinerary = Itinerary::find($tour_id);
$itinerary->plan = $temp_itinerary[$i];        
$itinerary->day = $temp_day[$i];
$itinerary->save();

这篇关于从动态创建的输入字段将数据更新到表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆