gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:25:18 25 4
gpt4 key购买 nike

我在模型代码中使用来获取关系

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

扩展程序隐藏了密码字段。

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

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

我怎样才能实现这个目标?

最佳答案

->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']);
});

关于Laravel - 如何使 Laravel 关系中的属性可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44167733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com