简体   繁体   English

如何使用特定的列值更新laravel中的表

[英]How to update a table in laravel with a specific column value

Please, some help I'm trying a different ways to update an especific row. 请提供一些帮助,我正在尝试其他方法来更新especific行。 Im trying with models, I posted my first attempt. 我正在尝试模型,我发布了第一次尝试。 That is not good... The other option is to update each column, but, will be usefull has other ideas or options... 那不好。另一种选择是更新每列,但是有其他想法或选择会很有用...

Route::put('/usuario/{fk_id_tid}/{ident}', function (Request $request, $fk_id_tid, $ident) {

$validator = Validator::make($request->all(), [
    'ident' => 'required|digits_between:5,12',
    'fname' => 'required|max:20',
    'mname' => 'required|max:20',
    'lname' => 'required|max:20',
    'lname2' => 'required|max:20',
    'email' => 'email',
    'telefono' => 'min:7',
    'celular' => 'min:10',
]);

if ($validator->fails()){
    return redirect('/usuarios')
        ->withErrors($validator)
        ->withInput();

}        

$user =  DB::table('usuario')->where('fk_id_tid', $fk_id_tid)->where('ident', $ident)->first();  
$user->fk_id_tid = $request->id_tid;
$user->ident = $request->ident; 
$user->fname = $request->fname;
$user->mname = $request->mname;
$user->lname = $request->lname;
$user->lname2 = $request->lname2;
$user->email = $request->email;
$user->telefono = $request->telefono;
$user->celular = $request->celular;

$user->save();

return redirect('/usuarios');

});

the information of the columns comes with a post method. 列的信息带有post方法。

Thanks for any help... 谢谢你的帮助...

The other way is 另一种方法是

$user =  DB::table('usuario')->where('fk_id_tid', $fk_id_tid)->where('ident', $ident)->first();  

$user->fk_id_tid = $request->id_tid;

if($request->ident)
{
  $user->ident = $request->ident;
} 

......

if($request->celular)
{
   $user->celular = $request->celular;
}

$user->save();

This updates specific columns depends upon inputs 此更新特定列取决于输入

Use next syntax instead of fetching and saving: 使用下一种语法而不是获取和保存:

DB::table('usuario')
    ->where('fk_id_tid', $fk_id_tid)
    ->where('ident', $ident)
    ->update([
        'mname' => $request->mname,
        'lname' => $request->lname2,
        'email' => $request->email,
        'telefono' => $request->telefono,
        'celular' => $request->celular,
    ]
);
 DB::table('usuario')
   ->where('fk_id_tid', $fk_id_tid)
   ->where('ident', $ident)
   ->update([
    'mname' => $request->mname,
    'lname' => $request->lname2,
    'email' => $request->email,
    'telefono' => $request->telefono,
    'celular' => $request->celular,
  ]
);

or you can use model , just define your all fillable field in model and write query like this. 或者您可以使用model,只需在model中定义所有可填充字段并编写查询即可。

       Usuario::where('fk_id_tid', $fk_id_tid)
      ->where('ident', $ident)
    ->update([
    'mname' => $request->mname,
   'lname' => $request->lname2,
  'email' => $request->email,
  'telefono' => $request->telefono,
  'celular' => $request->celular,
   ]
);

//Usuario your model name , define your all field in the model like //使用您的模型名称,在模型中定义您的所有字段,例如

public $fillable = ['add your all filable field here'];

Try this. 尝试这个。 Hope it will help you.Thanks. 希望对您有所帮助。

$updateDetails=array(
  'fk_id_tid' => $request->id_tid;
  'ident'     => $request->ident; 
  'fname' => $request->fname;
  'mname' => $request->mname;
  'lname' => $request->lname;
  'lname2' => $request->lname2;
  'email' => $request->email;
  'telefono' => $request->telefono;
  'celular' => $request->celular;
);

DB::table('usuario')
    ->where('fk_id_tid', $fk_id_tid)
    ->where('ident', $ident)
    ->update($updateDetails);

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

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