1

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

3
  • And why is it "not good"? Commented Apr 18, 2018 at 7:19
  • Do you have Usuario model? Commented Apr 18, 2018 at 7:24
  • Yes I have an Usuario Model, Commented Apr 19, 2018 at 1:56

4 Answers 4

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

I try 3 ways, the best one was the Nikita option, The other two present some kind of dificulty when we run the querys...
0

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

Comments

0

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

Comments

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

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'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.