Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel How to display $hidden attribute on model on paginate

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.

like image 384
Goper Leo Zosa Avatar asked Feb 07 '18 02:02

Goper Leo Zosa


2 Answers

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']);
like image 169
Niraj Shah Avatar answered Oct 02 '22 11:10

Niraj Shah


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;
like image 42
Star lin Avatar answered Oct 02 '22 13:10

Star lin