Laravel:仅在某些路线中隐藏模型的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36612164/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:36:19  来源:igfitidea点击:

Laravel: Hide attributes from model just in some routes

phplaravelmodelhide

提问by CanKer DiAlike

how do I hide some attributes from the model in just some routes, for example:

如何仅在某些路由中隐藏模型中的某些属性,例如:

I'm using protected $hidden to hide elements but this hide in all my functions or restful routes (index, show)

我正在使用 protected $hidden 来隐藏元素,但这隐藏在我的所有功能或宁静路线中(索引、显示)

 $hidden = [
  'coachVisibility', 'thumbnail', 'studentVisibility',
  'isHTML', 'studentIndex', 'coachIndex',
  'isURL', 'source', 'path',
  'status', 'updateTime', 'isfolder',
  'parentResource', 'idModifierUser', 'idResourceType',
  'idCreatorUser', 'idCreationCountry', 'user',
  'country', 'resource'
];

I want to hide only in Index function but in show function I don't want to hide anything.

我只想在 Index 函数中隐藏,但在 show 函数中我不想隐藏任何东西。

回答by Joseph Silber

You can use the addHiddenmethod on the models:

您可以addHidden在模型上使用该方法:

class UsersController
{
    public function index ()
    {
        return User::all()->each(function ($user) {
            $user->addHidden([.........]);
        });
    }
}

Once this PRgets merged, you'll be able to call it directly on the collection:

一旦这个 PR被合并,你就可以直接在集合上调用它:

class UsersController
{
    public function index ()
    {
        return User::all()->makeHidden([.........]);
    }
}


According to your comment, you can keep all of those fields in the $hiddenproperty of your model, and instead make them visible only in the showmethod:

根据您的评论,您可以将所有这些字段保留在$hidden模型的属性中,而是仅在show方法中使它们可见:

public function show($id)
{
    return CTL_Resource::where('idResource', $id)
        ->with('tags', 'quickTags', 'relatedTo')
        ->firstOrFail()->makeVisible([
            'coachVisibility', 'thumbnail', 'studentVisibility'
        ]);
}

回答by Angad Dubey

Consider using Transformers to transform return data as you would like.

考虑使用 Transformers 根据需要转换返回数据。

For eg:

例如:

Create an abstract Transformer:

创建一个抽象的 Transformer:

namespace App\Transformers;

abstract class Transformer
{

    public function transformCollection(array $items)
    {
        return array_map([$this, 'transform'], $items);
    }

    public abstract function transform($item);
}

Then create custom transformers for each method if you like:

如果您愿意,然后为每种方法创建自定义转换器:

namespace App\Transformers;

use App\User;

class UserTransformer extends Transformer {

    public function transform($user) {

         return [
             'custom_field' => $user['foo'],
             'another_custom_field' => $user['bar']
             ...
        ];

    }
}

Then in your controller:

然后在您的控制器中:

...

public function index(Request $request, UserTransformer $transformer)
{
    $users = User::all();

    return response()->json([
        'users' => $transformer->transformCollection($users->toArray())
     ], 200);
}

There are a couple of advantages to this:

这样做有几个优点:

  1. You can show/hide data as you wish with specific transformers
  2. In-case you decide to change the column names in your table, your transformers will ensure that clients getting your data will not break.
  1. 您可以根据需要使用特定转换器显示/隐藏数据
  2. 如果您决定更改表中的列名,您的转换器将确保客户端获取您的数据不会中断。