Laravel仅更新单行

时间:2017-05-14 16:14:01

标签: php laravel record

我在使用laravel更新数据库中的单个记录时遇到了问题。

当我运行此方法并仅提供请求参数时,请输入#34; name"那么其他字段在数据库中将是空白的。 如何保留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);
    }
}

5 个答案:

答案 0 :(得分:4)

您可以直接将update方法与请求输入一起使用。这只会更新提供的输入。

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

但很少有事情需要注意。您正尝试直接从输入更新密码。这是一种不好的做法,可能是错误的,因为laravel默认使用bcrypt来存储密码。其次,请确保在模型中设置$fillable属性以防止批量分配。

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

你也可以使用mutator来密码密码。

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

答案 1 :(得分:2)

你可以通过这样的方式使用默认值(请记住模型中的$ fillable)。

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

将模型中的mutator设置为Hash密码。 :)

祝你好运:D

答案 2 :(得分:1)

只需检查请求数据是否为空,例如:

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

答案 3 :(得分:1)

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

?>

答案 4 :(得分:0)

您可以这样操作。(“ posts”是数据库表的名称)

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