简体   繁体   中英

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. 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...

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.

       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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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