anduplats
anduplats

Reputation: 1003

Laravel update only single row

I am having a trouble updating my single record in database with laravel.

When i run this method and give requestparams only "name" then the other fields are going to be blank in database. How to keep the values that are not specified in the requestparams?.

public function update(Request $request, $id)
{
    $user = User::find($id);
    if(!is_null($user)){
        $user->name = $request->input('name');
        $user->email = $request->input('email');
        $user->password = $request->input('password');
        $user->save();
    }else{
        $data = array('msg' => 'The user, you want to update, does not exist', 'error' => true);
        echo json_encode($data);
    }
}

Upvotes: 5

Views: 24098

Answers (5)

Jitender Sharma
Jitender Sharma

Reputation: 141

You can do that like this.(Here 'posts' is Database Table name)

// write following statement in your model.
    use DB; 

     public function update($request, $id){
                $check = DB::Table('posts')->where('id',$id)->first(); 
                if(!is_null($check)){
                    $result =  DB::Table('posts')->where('id',$id)->update(
                    array(
                    'name' =>  $request->name,
                    'email' => $request->email,
                    'password' => $request->password
                    )
                    );
                    return $result = array('msg' => 'Updated successfully !! ', 'success' => true);
                }
                else{
                    return $result = array('msg' => 'User Not Found !! ', 'error' => true);
                }
            }

Upvotes: 0

<?php

use Illuminate\Support\Facades\Input;
use Hash;
use App\User;
use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class RegistrationController extends Controller
{

public function validator(array $data){ 

    return Validator::make($data, [
         'name' => 'required|max:255', //  Name
         'email' => 'required|email|max:255|unique:users', // Unique Email id
         'password' => 'required|min:6', //password min 6 charater

     ]);
  }



public function update(Request $request, $id)
{

 /* Called Validator Method For Validation */  
   $validation = $this->validator($request->all());


    $User = User::where('id',$id)->first(); /* Check id exist in table */

      if(!is_null($User)){

        $input = $request->all();
        $name = $input['name'];
        $email = $input['email'];
        $password = Hash::make($input['password']);

        User::where('id',$id)->update(
         array(
                 'name' => $name,
                 'email' => $email,
                 'password' => $password,
              )
         );


          $data = array('msg' => 'Updated successfully !! ', 'success' => true);
          echo json_encode($data);

    }else{

        $data = array('msg' => 'User Not Found !! ', 'error' => true);
        echo json_encode($data);
    }
}

?>

Upvotes: 1

Sandeesh
Sandeesh

Reputation: 11906

You can use the update method with the request input directly. This will only update the inputs provided.

$user->update($request->only(['name', 'email', 'password']));

Few things to note though. You're trying to update the password directly from the input. Which is a bad practise and might be wrong since laravel uses bcrypt by default to store passwords. Secondly make sure you set the $fillable property in the model to protect against mass-assignment.

protected $fillable = [
    'name', 'email', 'password'
];

Also you can use a mutator to hash the password like so.

public function setPasswordAttribute($value)
{
    $this->attributes['password'] = bcrypt($value);
}

Upvotes: 7

Patryk Woziński
Patryk Woziński

Reputation: 746

You can do that by something like this with default values (remember about $fillable in your model).

$user->update([
    'name' =>  $request->input('name', $user->name),
    'email' => $request->input('email', $user->password),
    'password' => $request->input('password', $user->password),
]);

Set your mutator in model to Hash password. :)

Good luck mate :D

Upvotes: 3

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

Just check if request data is empty, for example:

$user->name = empty($request->name) ? $user->name : $request->name;
$user->email = empty($request->email) ? $user->email : $request->email;
$user->password = empty($request->password) ? $user->password : bcrypt($request->password);

Upvotes: 1

Related Questions