$hidden 在 eloquent 模型中到底有什么影响?
What effect does $hidden have exactly in eloquent model?
我目前正在摆弄 Lumen,我正在使用 eloquent 进行我的数据库交互。
我已经通读了 Eloquent 的文档,其中有关于隐藏属性的解释:
有时您可能希望限制模型数组或 JSON 表示中包含的属性,例如密码。为此,请将 $hidden 属性 添加到您的模型中:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = ['password'];
}
Alternatively, you may use the visible property to define a white-list of attributes that should be included in your model's array and JSON representation. All other attributes will be hidden when the model is converted to an array or JSON:
我不明白这有什么含义。如果我查询插入密码的位置,我应该隐藏它吗?或者这会导致密码根本不出现在我的模型实例中吗?
例如,我有以下用户模型:
<?php
namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Auth\Authorizable;
class User extends Model implements AuthenticatableContract, AuthorizableContract
{
use Authenticatable, Authorizable;
//protected $table = 'user';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'role'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password'];
public $timestamps = false;
}
我现在是 运行 控制器,它将新用户的姓名、电子邮件、密码和角色插入用户 table。
在这里你可以看到table:
https://imgur.com/8r2JjPh
现在,当访问我的模型以插入一个新行时,如下所示:
User::create($requestData);
出了点问题...
密码没有被插入。
我调试了输入,数据在那里,插入发生之前输入的 JSON 字符串如下所示:
{"name":"tester1","email":"test.tester1@tested.de","password":"3627909a29c31381a071ec27f7c9ca97726182aed29a7ddd2e54353322cfb30abb9e3a6df2ac2c20fe23436311d678564d0c8d305930575f60e2d3d048184d79","role":"Benutzer"}
使用 php 函数 hash("sha512", $password);
对密码进行了哈希处理。它基于“12345”,仅用于测试 :D :P
正如预期的那样,散列密码具有所需的 128 个字符长度。
知道此行为是否是由模型中定义为隐藏的密码属性引起的吗?
编辑:
这就是我散列密码的方式:
$requestData["password"] = hash("sha512", $requestData["password"]);
密码不会被插入,因为您的 $fillable
数组中没有 password
。
$fillable
数组是为了防止批量赋值。如果您是 "filling" 数组中的模型属性,则需要将属性名称添加到该数组中。
话虽如此,我实际上建议您不要将 password
添加到 $fillable
数组,而是在模型上明确设置密码:
$user = new User($requestData);
$user->password = $requestData["password"];
$user->save();
如评论中所述,$hidden
属性仅用于将模型转换为数组或转换为 JSON 时,因此它不应该对插入(或任何东西)产生影响否则)。
protected $hidden 是一个数组,是一个模型 class 参数,它的作用是在查询结果中隐藏数据库中的列(在数组中)。在您的示例中,$hidden = ['password'] 使用户结果中的 'password' 列不可见。
https://laravel.com/api/6.x/Illuminate/Database/Eloquent/Model.html 'protected array $hidden The attributes that should be hidden for serialization.'
我目前正在摆弄 Lumen,我正在使用 eloquent 进行我的数据库交互。 我已经通读了 Eloquent 的文档,其中有关于隐藏属性的解释:
有时您可能希望限制模型数组或 JSON 表示中包含的属性,例如密码。为此,请将 $hidden 属性 添加到您的模型中:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = ['password'];
}
Alternatively, you may use the visible property to define a white-list of attributes that should be included in your model's array and JSON representation. All other attributes will be hidden when the model is converted to an array or JSON:
我不明白这有什么含义。如果我查询插入密码的位置,我应该隐藏它吗?或者这会导致密码根本不出现在我的模型实例中吗?
例如,我有以下用户模型:
<?php
namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Auth\Authorizable;
class User extends Model implements AuthenticatableContract, AuthorizableContract
{
use Authenticatable, Authorizable;
//protected $table = 'user';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'role'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password'];
public $timestamps = false;
}
我现在是 运行 控制器,它将新用户的姓名、电子邮件、密码和角色插入用户 table。 在这里你可以看到table: https://imgur.com/8r2JjPh
现在,当访问我的模型以插入一个新行时,如下所示: User::create($requestData);
出了点问题... 密码没有被插入。 我调试了输入,数据在那里,插入发生之前输入的 JSON 字符串如下所示:
{"name":"tester1","email":"test.tester1@tested.de","password":"3627909a29c31381a071ec27f7c9ca97726182aed29a7ddd2e54353322cfb30abb9e3a6df2ac2c20fe23436311d678564d0c8d305930575f60e2d3d048184d79","role":"Benutzer"}
使用 php 函数 hash("sha512", $password);
对密码进行了哈希处理。它基于“12345”,仅用于测试 :D :P
正如预期的那样,散列密码具有所需的 128 个字符长度。
知道此行为是否是由模型中定义为隐藏的密码属性引起的吗?
编辑: 这就是我散列密码的方式:
$requestData["password"] = hash("sha512", $requestData["password"]);
密码不会被插入,因为您的 $fillable
数组中没有 password
。
$fillable
数组是为了防止批量赋值。如果您是 "filling" 数组中的模型属性,则需要将属性名称添加到该数组中。
话虽如此,我实际上建议您不要将 password
添加到 $fillable
数组,而是在模型上明确设置密码:
$user = new User($requestData);
$user->password = $requestData["password"];
$user->save();
如评论中所述,$hidden
属性仅用于将模型转换为数组或转换为 JSON 时,因此它不应该对插入(或任何东西)产生影响否则)。
protected $hidden 是一个数组,是一个模型 class 参数,它的作用是在查询结果中隐藏数据库中的列(在数组中)。在您的示例中,$hidden = ['password'] 使用户结果中的 'password' 列不可见。
https://laravel.com/api/6.x/Illuminate/Database/Eloquent/Model.html 'protected array $hidden The attributes that should be hidden for serialization.'