16

I'm using Laravel 5.5. I read about this and know this function and it works makeVisible

$hidden = ['password', 'remember_token', 'email'];

I can display email using

$profile = auth()->user()->find($request->user()->id);
$profile->makeVisible(['email']);

On the frontend email is displayed. But it not works on many results like

 // Get all users
 $users = User::with('role', 'level')->makeVisible(['email'])->paginate(10); // Doesn't work

Also try this method from Laracasts toJson it works but I can't do it using paginate. Can you provide other methods or how to solve this? My aim is to display email column that is hidden. Thanks.

Goper Leo Zosa
  • 1,185
  • 3
  • 15
  • 33
  • Try using [`setVisible`](https://laravel.com/api/5.3/Illuminate/Database/Eloquent/Model.html#method_setVisible). –  Feb 07 '18 at 02:48
  • Yes I've tried it. Still doesn't work `$users = User::with('role', 'level')->makeVisible(['email'])->setVisible('email')->paginate(10);` `setVisible` and `setHidden` only works when the model is first load. I already try that method. – Goper Leo Zosa Feb 07 '18 at 02:50
  • https://github.com/laravel/framework/issues/16501 – Nate Feb 07 '18 at 03:29

4 Answers4

15

Another, possible easier solution depending on your requirements, is to call makeVisible on the collection:

// Get all users
$users = User::with('role', 'level')->paginate(10)->makeVisible(['email']);

You can also use this with find or get:

$profile = auth()->user()->find($request->user()->id)->makeVisible(['email']);
Niraj Shah
  • 15,087
  • 3
  • 41
  • 60
12

I solve this using this method.

Users.php on model

public function toArray()
{
    // Only hide email if `guest` or not an `admin`
    if (auth()->check() && auth()->user()->isAdmin()) {
        $this->setAttributeVisibility();
    }

    return parent::toArray();
}

public function setAttributeVisibility()
{
    $this->makeVisible(array_merge($this->fillable, $this->appends, ['enter_relationship_or_other_needed_data']));
}

and on controller just a simple

return User::with('role', 'level')->paginate(10);

I've read where pagination comes from toArray before creating pagination. Thanks for all your help. Also helps

Mansouri
  • 233
  • 4
  • 6
Goper Leo Zosa
  • 1,185
  • 3
  • 15
  • 33
8

You can use this:

    $paginator = User::with('role', 'level')->paginate($pageSize);
    $data = $pagination->getCollection();
    $data->each(function ($item) {
        $item->setHidden([])->setVisible(['email']);
    });
    $paginator->setCollection($data);
    return $paginator;
Star lin
  • 81
  • 1
  • 1
3

You can try to use this approach. Using API Resources.

API Resources lets you format the data the way you want. You can create multiple Resource object to format in different ways your collections.

Set visible your parameter (in this case email) and when you need to return that item you can use a different Resource object that returns that elemement.

So when no need for email:

$users = User::with('role', 'level')->paginate(10);
return UserWithoutEmail::collection($users);

when email is needed:

$users = User::with('role', 'level')->paginate(10);
return UserWithEmail::collection($users);
Kenny Horna
  • 13,485
  • 4
  • 44
  • 71