【问题标题】:Laravel - How to Update two valuesLaravel - 如何更新两个值
【发布时间】:2019-01-09 09:07:25
【问题描述】:

我想更新数据中的两个值我想将第一个值更新为 int,另一个是字符串。

表格结构

这是我的输入,值aircraft_id 是第一个要更新的值,另一个是aircraft_refistration_number

 <select name="aircraft_id" class="form-control" id="">
    <option value="0" disabled="true" selected="true"> Select </option>
       @foreach ($aircrafts as $aircraft)

    <option value="{{ $aircraft->aircraft_id }}">{{ $aircraft->aircraft_registration_number }}</option>

       @endforeach
  </select>

这是我的桌子

这是我更新 registered_company_name 的地方,它必须是字符串,但输出是 aircraft_id

$txtDescript = $request->input('aircraft_id');

  $aircraft = DB::table('settings')
  ->where('id', 4)
  ->update(['description' => $txtDescript]);

这里是 aircraft_id,应该是 int 或 id 的

  $airid = $request->input('aircraft_id');

    $aircraft = DB::table('series')
    ->update(['aircraft_id' => $airid]);

这个效果很好

这是我对问题的输出

WHEREAS 应该是这样的输出,应该是正确的

【问题讨论】:

  • @OluwatobiSamuelOmisakin no sir 1 是aircraft_id 正确的更新值应该是Incorporated Airlines 看上面我的表字段值的值
  • 为什么是这条线? $txtDescript = $request-&gt;input('aircraft_id');
  • @SummerWinter 什么样的代码?我建议你先学习foreign keys
  • @vivek_23 请给我内部连接表的样本,它必须更新我当前的表和另一个表,请先生,然后我就从那里学习,非常感谢
  • @SummerWinter 给我看aircraft 表。另外,您显示的表格的名称是什么?

标签: php sql laravel laravel-5


【解决方案1】:

您可以采用这种更长的形式,

 <select name="aircraft_id" class="form-control" id="">
    <option value="0" disabled="true" selected="true" id="airselect"> Select </option>
       @foreach ($aircrafts as $aircraft)

    <option value="{{ $aircraft->aircraft_id }}">{{ $aircraft->aircraft_registration_number }}</option>
       @endforeach
  </select>
<input type="hidden" name="aircraft_name" id="aircraft_name" value="">

并像这样使用 jquery 设置隐藏字段的值

$(document).ready(function(){
$(document).on('change','.airselect',function(){
$selected=$( "#airselect option:selected" ).text();
$('#aircraft_name').val($selected);
});
});

然后像这样调整你的控制器

$txtDescript = $request->input('aircraft_name');
$aircraft = DB::table('settings')
  ->where('id', 4)
  ->update(['
description' => $txtDescript]);

$airid = $request->input('aircraft_id');

$aircraft = DB::table('series')
->update(['aircraft_id' => $airid]);

试试看,让我从你这里,我没有测试代码

【讨论】:

  • 您好,谢谢您的回答,您确定隐藏的输入类型在select输入内吗?
  • 你好,它没有用,但你是对的,我需要一个 jquery 或 ajax 来获取其他值
  • 对不起,输入注释放在select里面,这是我的错误,它应该在
猜你喜欢
  • 2023-02-03
  • 1970-01-01
  • 2021-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-03
  • 1970-01-01
  • 2019-01-19
相关资源
最近更新 更多