Laravel - 如何使 Laravel 关系中的属性可见?

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

Laravel - how to makeVisible an attribute in a Laravel relation?

laravel

提问by Gruz

I use in my model code to get a relation

我在我的模型代码中使用以获得关系

class User extends Authenticatable
{
    // ...
    public function extensions()
    {
        return $this->belongsToMany(Extension::class, 'v_extension_users', 'user_uuid', 'extension_uuid');
    }
    // ...
}

The Extensionhas field passwordhidden.

扩展名具有现场密码隐藏。

class Extension extends Model
{
    // ...
    protected $hidden = [
        'password',
    ];
    // ...
}

Under some circumstances I want to makeVisiblethe password field.

在某些情况下,我想让密码字段可见

How can I achieve this?

我怎样才能做到这一点?

采纳答案by Gruz

Well, I got the idea from https://stackoverflow.com/a/38297876/518704

好吧,我从https://stackoverflow.com/a/38297876/518704得到了这个想法

Since my relation model Extension::classis called by name in my code return $this->belongsToMany(Extension::class,...I cannot even pass parameter to it's constructor.

由于我的关系模型Extension::class在我的代码中按名称调用,因此我return $this->belongsToMany(Extension::class,...什至无法将参数传递给它的构造函数。

So to pass something to the constructor I may use static class variables.

因此,要将某些内容传递给构造函数,我可能会使用静态类变量。

So in my Extensionmodel I add static variables and run makeVisible method. Later I destruct the variables to be sure next calls and instances use default model settings.

所以在我的Extension模型中,我添加了静态变量并运行 makeVisible 方法。后来我破坏了变量以确保下一次调用和实例使用默认模型设置。

I moved this to a trait, but here I show at my model example.

我把它移到了一个特征,但在这里我展示了我的模型示例。

class Extension extends Model
{
    public static $staticMakeVisible;

    public function __construct($attributes = array())
    {
      parent::__construct($attributes);

      if (isset(self::$staticMakeVisible)){
          $this->makeVisible(self::$staticMakeVisible);
      }
   }
.....

    public function __destruct()
    {
      self::$staticMakeVisible = null;
    }

}

And in my relation I use something like this

在我的关系中我使用这样的东西

class User extends Authenticatable
{
...
    public function extensions()
    {
        $class = Extension::class;
        $class::$staticMakeVisible = ['password'];

        return $this->belongsToMany(Extension::class, 'v_extension_users', 'user_uuid', 'extension_uuid');
    }
...
}

回答by DevK

->makeVisible([...])should work:

->makeVisible([...])应该管用:

$model = \Model::first();
$model->makeVisible(['password']);

$models = \Model::get();
$models = $models->each(function ($i, $k) {
    $i->makeVisible(['password']);
});

// belongs to many / has many
$related = $parent->relation->each(function ($i, $k) {
    $i->makeVisible(['password']);
});

// belongs to many / has many - with loading
$related = $parent->relation()->get()->each(function ($i, $k) {
    $i->makeVisible(['password']);
});

回答by Thom Seddon

The highest voted answer didn't seem to work for me (the relationsattribute seems to be a protected arraynow so can't be used as a collection in @DevK's answer), I instead used:

投票最高的答案似乎对我不起作用(该relations属性现在似乎是受保护的数组,因此不能在@DevK 的答案中用作集合),我改为使用:

$parent->setRelation('child', $parent->child->first()->setVisible(['id']));