According to http://laravel.com/docs/eloquent, one can Hide Attributes From Array Or JSON Conversion by using a protected $hidden variable in the Model.

class User extends Eloquent {
    protected $hidden = array('password');
}

Great, however when running print_r(User::all()) the encrypted password is sent from server to client inside the User object.

这不仅限于print_r(),如果查询特定用户,$user->password将在视图中显示加密的密码.

Is there a way of stopping this? Every time my user object is queried, the password will sent with it as part of the data, even though it doesn't need to be.

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
                    )

推荐答案

When you run User::all(), it returns a Collection object. This Collection contains all your Users in object form. Therefore, your Users will contain their passwords. This is so you can display the hashed password for whatever reason. However, as you said before, if you transform the Collection or Users into arrays or JSON, the password field should be gone if hidden.

因此,如果您想要删除它们,请try 运行以下命令:

$array_of_users = Users::all()->toArray();
$json_of_users = Users::all()->toJson();

dd() these both to inspect them. The password field will be gone.

This is explained in Laravel's documentation on serialization.

Laravel相关问答推荐

如何在 Laravel 中使用多对一变形关系

Laravel 模型事件:delete() 不会从存储中删除文件

在 laravel 项目中放置 css 文件的位置

Laravel 5.3:语法错误或访问冲突:1463 HAVING 子句中使用了非分组字段距离

Laravel 在维护模式下显示自定义消息

带有 Laravel Passport 的 SSO

如何从 laravel 控制器执行外部 shell 命令?

artisan 迁移错误找不到类'Doctrine\\DBAL\\Driver\\PDOMySql\\Driver'

Laravel 5.5 类型错误:传递给 Illuminate\Auth\EloquentUserProvider::validateCredentials() 的参数 1 必须是实例

使用数据库中的值进行动态邮件配置 [Laravel]

为什么客户凭证应该与 Laravel Passport 中的用户相关联?

无法打开流:没有这样的文件或目录(Laravel)

Laravel 全部返回 ID 变为 0

控制器外部的 Laravel 访问请求对象

如何将 Facebook PHP SDK 与 Laravel 5.4 集成?

Laravel Mix 生成字体到另一个目录

Class类 App\Http\Controllers\Auth\Request 不存在.

Laravel 5 - 判断日期是否是今天

基于日期的 Laravel 日志(log)文件

如果参数不是整数,如何以不同方式定义路由