根据http://laravel.com/docs/eloquent,可以通过在模型中使用受保护的 $hidden 变量来隐藏数组或 JSON 转换中的属性。
class User extends Eloquent {
protected $hidden = array('password');
}
很好,但是在运行时print_r(User::all())
,加密密码会在用户对象内从服务器发送到客户端。
这不仅限于 print_r(),如果查询特定用户,$user->password
将在视图中显示加密密码。
有没有办法阻止这种情况?每次查询我的用户对象时,密码都会作为数据的一部分与它一起发送,即使它不需要这样做。
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => User Object
(
[hidden:protected] => Array
(
[0] => password
)
[connection:protected] =>
[table:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 1
[email] => admin@admin.com
[first_name] => Admin
[last_name] => User
[password] => $2y$10$7Wg2Wim9zHbtGQRAi0z6XeapJbAIoh4RhEnVXvdMtFnwcOh5g/W2a
[permissions] =>
[activated] => 1
[activation_code] =>
[activated_at] =>
[last_login] =>
[persist_code] =>
[reset_password_code] =>
[created_at] => 2013-09-26 10:24:23
[updated_at] => 2013-09-26 10:24:23
)