Laravel 如何在分页上的模型上显示 $hidden 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48655161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel How to display $hidden attribute on model on paginate
提问by Goper Leo Zosa
I'm using Laravel 5.5. I read about this and know this function and it works makeVisible
我正在使用 Laravel 5.5。我读到了这个并且知道这个函数并且它可以工作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 toJsonit 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.
也可以尝试从 Laracasts 到 Json 的这种方法它有效,但我不能使用 paginate 来做到这一点。你能提供其他方法或如何解决这个问题吗?我的目标是显示email
隐藏的列。谢谢。
采纳答案by Goper Leo Zosa
I solve this using this method.
我用这个方法解决了这个问题。
Users.php
on model
Users.php
在模型上
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);
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
toArray
在创建分页之前,我已经阅读了分页的来源。感谢你的帮助。也有帮助
回答by Star lin
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;
回答by Kenny Horna
You can try to use this approach. Using API Resources.
您可以尝试使用这种方法。使用API 资源。
API Resources lets you format the data the way you want. You can create multiple Resource object to format in different ways your collections.
API 资源让您可以按照自己的方式格式化数据。您可以创建多个 Resource 对象以不同方式设置您的集合的格式。
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.
设置可见您的参数(在本例中email
),当您需要返回该项目时,您可以使用不同的 Resource 对象来返回该元素。
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);