Laravel 只更新单行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43966149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel update only single row
提问by anduplats
I am having a trouble updating my single record in database with laravel.
我在使用 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?.
当我运行此方法并仅给 requestparams 提供“名称”时,数据库中的其他字段将变为空白。如何保留 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);
}
}
回答by Sandeesh
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.
不过要注意的事情很少。您正在尝试直接从输入中更新密码。这是一个不好的做法,可能是错误的,因为 laravel 默认使用 bcrypt 来存储密码。其次,确保您$fillable
在模型中设置了属性以防止批量分配。
protected $fillable = [
'name', 'email', 'password'
];
Also you can use a mutator to hash the password like so.
您也可以使用 mutator 像这样散列密码。
public function setPasswordAttribute($value)
{
$this->attributes['password'] = bcrypt($value);
}
回答by Patryk Woziński
You can do that by something like this with default values (remember about $fillable in your model).
您可以使用默认值(记住模型中的 $fillable )通过类似这样的方法来做到这一点。
$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. :)
将模型中的 mutator 设置为哈希密码。:)
Good luck mate :D
祝你好运 :D
回答by Alexey Mezenin
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);
回答by R?sh?Kêsh Kümar
<?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);
}
}
?>
回答by Jitender Sharma
You can do that like this.(Here 'posts' is Database Table name)
你可以这样做。(这里的“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);
}
}