请帮助我,我正在尝试不同的方法来更新特定的行。我试用模特,我发布了我的第一次尝试。这不好......另一个选择是更新每一列,但是,有用的还有其他想法或选项......
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');
});
列的信息附带一个post方法。
感谢您的帮助......
答案 0 :(得分:0)
另一种方式是
$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();
此更新特定列取决于输入
答案 1 :(得分:0)
使用下一个语法而不是提取和保存:
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,
]
);
答案 2 :(得分:0)
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,
]
);
或 您可以使用模型,只需在模型中定义所有可填写字段并像这样编写查询。
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您的模型名称,在模型中定义您的所有字段,如
public $fillable = ['add your all filable field here'];
答案 3 :(得分:0)
试试这个。希望它会对你有所帮助。谢谢。
$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);