【问题标题】:Laravel: 'null value in column "password" violates not-null constraint' even though password is definitely providedLaravel:即使明确提供了密码,“密码”列中的空值也违反了非空约束
【发布时间】:2017-09-23 14:20:14
【问题描述】:

我正在 Laravel 5.5 中构建一个 GraphQL API,我从 User::create() 收到一个错误,说我没有为“密码”列提供值,尽管我确实提供了。这是来自User::create()的完整消息:

SQLSTATE[23502]: Not null violation: 7 ERROR:  null value in column \"password\" violates not-null constraint
DETAIL:  Failing row contains (507, null, null, 2017-09-23 14:12:11, 2017-09-23 14:12:11, 658495, 56854685, Joe, Appleseed, royal, 1970-01-01, +31684736248, null, Poplar St, 14a, 1012AB, London, null, joe.appleseed@overwatch.com, joe.appleseed@mindef.nl, null). (SQL: insert into \"users\" (\"wid\", \"unumber\", \"first_name\", \"last_name\", \"civil_status\", \"birthdate\", \"phone_number\", \"street\", \"street_number\", \"postal_code\", \"city\", \"email_address_overwatch\", \"email_address_mindef\", \"updated_at\", \"created_at\") values (658495, 56854685, Joe, Appleseed, royal, 1970-01-01, +31684736248, Poplar St, 14a, 1012AB, London, joe.appleseed@overwatch.com, joe.appleseed@mindef.nl, 2017-09-23 14:12:11, 2017-09-23 14:12:11) returning \"id\")

这是生成它的代码,在我的 UserRepository 中:

/**
 * Creates a new User with $data
 *
 * @param array $data
 * @return User
 */
public function create(array $data): User
{
    $data['password'] = bcrypt($data['password']);
    $user = User::create($data);
    $this->log(Auth::user(), $user, 'created');

    return $user;
}

我觉得这非常奇怪,因为我肯定会为密码提供一个值。我可以通过使用dd($data) 看到这一点:(就在$data['password'] = bcrypt($data'password'); 下方

array:15 [
  \"wid\" => \"658495\"
  \"unumber\" => \"56854685\"
  \"first_name\" => \"Joe\"
  \"last_name\" => \"Appleseed\"
  \"civil_status\" => \"royal\"
  \"birthdate\" => \"1970-01-01\"
  \"phone_number\" => \"+31684736248\"
  \"street\" => \"Poplar St\"
  \"street_number\" => \"14a\"
  \"postal_code\" => \"1012AB\"
  \"city\" => \"London\"
  \"email\" => \"joe@appleseed.com\"
  \"email_address_overwatch\" => \"joe.appleseed@overwatch.com\"
  \"email_address_mindef\" => \"joe.appleseed@mindef.nl\"
  \"password\" => \"$2y$10$FdLbx/dYkdrjhGNcuCWKkO.bg013Gn0mhs7nKN.nyNztlykWx4jDC\"
]

如您所见,这个数组中肯定有一个password 字段。我怎么会收到这个错误?

一些环境信息:

PHP 7.1.9-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Sep  2 2017 05:56:43) ( NTS )
psql (PostgreSQL) 9.5.8
Laravel Framework 5.5.2

【问题讨论】:

  • 你不应该使用Hash::make($data['password'])吗?
  • @lawrencecherone 我不这么认为。标准的 laravel auth 脚手架使用 bcrypt

标签: php postgresql laravel eloquent


【解决方案1】:

我猜密码不是mass assignable。在您的用户模型中:

protected $fillable = [.... other fields ..., 'password'];

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-18
  • 2020-08-20
  • 2014-07-11
  • 2016-06-01
  • 2016-06-13
  • 2021-11-02
  • 1970-01-01
相关资源
最近更新 更多