简体   繁体   English

更新另一个表上的特定表字段

[英]Update specific table field on another Table

I need to update specific one id / row on a separate field on another table. 我需要在另一个表的单独字段上更新特定的一个ID /行。 how is the logic to that? 逻辑如何? so far i have this pseudo code sql query. 到目前为止,我有此伪代码sql查询。

table name : settings 表名设置

current table : aircrafts 当前表格飞机

UPDATE settings SET description id = 4 更新设置SET描述ID = 4

i need to update id 4 on my database in database settings 我需要在数据库settings更新数据库上的ID 4

how can i integrate this code into laravel syntax? 如何将这段代码集成到laravel语法中?

assuming i have this controller 假设我有这个控制器

    public function editAirReg(Request $request, $id)
{

    $descr = $request->input('descr_id');
    DB::update('update settings set description = ,'$descr', where id = 4',[$name,$id]);
    return redirect('/admin/aircrafts')->with('success', 'Settings Updated');
}

and also this view 还有这个观点

 {!! Form::open(['action' => 'Admin\AircraftsController@getdata', 'method' => 'POST']) !!}


   <input type="hidden" class="form-control" name="settings" value="{{$aircraft->air_line}}">


 {{Form::submit('BIND', ['class'=>'btn btn-primary btn-block btn-lg', 'name'=>'submit'])}}

 {!! Form::close() !!}

First of all, getData() doesn't make sense if you're updating something in mysql. 首先,如果要在mysql中进行更新,则getData()没有任何意义。 It should be called store() or save or whatever you want to call so that the function name is appropriate. 应该将其命名为store()或save或任何您想调用的名称,以便函数名合适。

Let's change the form so that it can pass the id to the controller. 让我们更改窗体,以便它可以将id传递给控制器​​。

{ Form::open(array('action' => 'Admin\AircraftsController@getdata', $id)) }}
    {{ Form::hidden('settings', $aircraft->air_line) }}
    {{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}

What I did is that change your input to the laravel syntax. 我所做的是将您的输入更改为laravel语法。 Also make sure you pass the id from form to controller. 还要确保将ID从表单传递到控制器。 take a look at my example. 看我的例子。 look where I put $id field. 看看我把$ id放在哪里。

Now in the controller, this is what you do. 现在在控制器中,这就是您要做的。

$setting = Setting::findOrFail($id); // you must have a model or you can use DB facade
$setting->description = $request->settings; //name that you called it in form
$setting->save(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM