Home > Enterprise >  Laravel 9 - Read/Get Request
Laravel 9 - Read/Get Request

Time:09-01

I'm beginning a full project with Laravel 9, I'm creating a users management page.

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. I want all informations except the password. How can I do that please ?

Thank you :)

CodePudding user response:

Use makeHidden function.

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

CodePudding user response:

Also, you can do like this.

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

CodePudding user response:

Changing your query to hide the password from response is a terrible idea since Laravel models include a default property.

Read it Hiding Attributes From JSON

Add below line in users model

  protected $hidden = ['password'];

and password will be hidden.

  • Related