Reputation: 67
Please, some help I'm trying a different ways to update an especific row. 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.
Thanks for any help...
Upvotes: 1
Views: 154
Reputation: 437
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);
Upvotes: 1
Reputation: 4040
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.
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'];
Upvotes: 0
Reputation: 10143
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,
]
);
Upvotes: 0
Reputation: 1004
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
Upvotes: 0