简体   繁体   English

Laravel 9 - 读取/获取请求

[英]Laravel 9 - Read/Get Request

I'm beginning a full project with Laravel 9, I'm creating a users management page.我正在使用 Laravel 9 开始一个完整的项目,我正在创建一个用户管理页面。

I would like to read all informations from 'users' table except the password.我想从“用户”表中读取除密码之外的所有信息。

    public function AffichageGestion()
    {
        $users = User::all();
        return view('usersManagement.userManagement')->with('users', $users);
    }

This is my code for the moment, I select all informations but I don't want this.这是我目前的代码,我 select 所有信息,但我不想要这个。 I want all informations except the password.我想要除密码之外的所有信息。 How can I do that please?请问我该怎么做?

Thank you:)谢谢:)

Use makeHidden function.使用 makeHidden function。

public function AffichageGestion()
    {
        $users = User::get()->makeHidden(['password']);
        return view('usersManagement.userManagement')->with('users', $users);
    }

Also, you can do like this.另外,你可以这样做。

public function AffichageGestion()
{
    $users = User::all([ 'name', 'email']); // except password
    return view('usersManagement.userManagement')->with('users', $users);
}

Changing your query to hide the password from response is a terrible idea since Laravel models include a default property.由于 Laravel 模型包含默认属性,因此更改查询以隐藏响应中的密码是一个糟糕的主意。

Read it Hiding Attributes From JSON 从 JSON 中读取隐藏属性

Add below line in users modelusers model 中添加以下行

  protected $hidden = ['password'];

and password will be hidden.并且密码将被隐藏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM