简体   繁体   English

Laravel 如何在分页上的模型上显示 $hidden 属性

[英]Laravel How to display $hidden attribute on model on paginate

I'm using Laravel 5.5.我正在使用 Laravel 5.5。 I read about this and know this function and it works makeVisible我读过这个并且知道这个函数并且它可以工作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.也可以尝试从 Laracasts 到 Json 的这种方法它可以工作,但我无法使用 paginate 来实现。 Can you provide other methods or how to solve this?你能提供其他方法或如何解决这个问题吗? My aim is to display email column that is hidden.我的目标是显示隐藏的email列。 Thanks.谢谢。

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);

I've read where pagination comes from toArray before creating pagination.在创建分页之前,我已经阅读了分页来自toArray地方。 Thanks for all your help.感谢您的帮助。 Also helps也有帮助

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;

You can try to use this approach.您可以尝试使用这种方法。 Using API Resources .使用API 资源

API Resources lets you format the data the way you want. API 资源让您可以按照自己的方式格式化数据。 You can create multiple Resource object to format in different ways your collections.您可以创建多个 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);

Another, possible easier solution depending on your requirements, is to call makeVisible on the collection:根据您的要求,另一个可能更简单的解决方案是在集合上调用makeVisible

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

You can also use this with find or get :您也可以将其与findget

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

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

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