gpt4 book ai didi

php - 流明 PHP : hidden password field in model not filled using Model->fill()

转载 作者:可可西里 更新时间:2023-11-01 17:26:01 28 4
gpt4 key购买 nike

我正在使用的 API 的用户注册代码一直存在问题。

我有一个 UserController,它获取发布的数据并验证字段。然后它将数据发送到我的 UserService 类中的 SignUpUser 方法,该方法创建用户模型并填写所有给定的数据并将其发送到 UserRepository,然后将其发送到我的数据库。

当我 dd() 我的 $password 它被填满但是当我 dd() 我填入 $user 字段返回null...不知道为什么还没有填写

我的代码截图:

用户 Controller

public function signUpUser(Request $request)
{
$this->validate($request,[
'username' => 'required|regex:/^[(a-zA-Z\s)(0-9\s)]+$/u|min:3',
'email' => 'required|email',
'password' => 'required|min:8',
'displayname' => 'required|regex:/^[(a-zA-Z\s)(0-9\s)]+$/u|min:3',
], [
'min' => 'The :attribute must at least be :min characters long.',
]);

$user = $this->userService->signUpUser(
$request->get('username'),
$request->get('email'),
$request->get('password'),
$request->get('displayname')
);



if($user)
{
//alright! user has been made
$response = ['status' => 'OK', 'message' => "User {$user->username} has successfully been created"];
}
else
{
//something went wrong
$response = ['status' => 'FAIL', 'message' => "Something went wrong or user already exists in DB"];
}

return response()->json($response, 201);
}

用户服务

public function signUpUser($userName, $email, $password, $displayName)
{
$password = password_hash($password, PASSWORD_DEFAULT);

$user = $this->userRepository->signUpNewUser($userName, strtolower($email), $password, $displayName);
$user = $this->userRepository->get($user->pk);
//TODO: send user signup mail soon
return $user;
}

用户库

public function signUpNewUser($username, $email, $password, $displayName)
{
$user = $this->getModel();
$user->fill(
[
'username' => $username,
'email' => $email,
'password' => $password,
'displayname' => $displayName,
]);
dd($user::all());
$user->save();
return $user;
}

用户模型

class UserModel extends BaseModel
{

/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';


/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'username', 'email', 'displayname'
];

/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
'password',
];
}

填写数据后转储$user Dumped $user after filling in data

最佳答案

将其添加到$fillable:

protected $fillable = ['username', 'email', 'displayname', 'password'];

此外,不要在数据库中保留未加密的密码,使用bcrypt():

'password' => bcrypt($password),

关于php - 流明 PHP : hidden password field in model not filled using Model->fill(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47381083/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com