Laravel/Eloquent - Eager loaded hidden/visible properties(Laravel/Eloquent - 急切加载隐藏/可见属性)
问题描述
在使用 Laravel 的 Eloquent ORM 时,我似乎无法在模型上动态设置 $hidden 和 $visible 属性.
When using Laravel's Eloquent ORM, I can't seem to set the $hidden and $visible properties on my Model dynamically.
示例 1:这有效:
class User extends Eloquent {
$this->visible = array('field_name');
function read()
{
return User::all();
}
}
示例 2:动态设置 Eloquent 类的可见属性,不起作用:
class User extends Eloquent {
function read($visible = array('field_name'))
{
$this->visible = $visible; // Also tried: $this->setVisible($visible);
return User::all();
}
}
示例 3:适用于模型本身的解决方案,但不适用于急切加载的模型:
class User extends Eloquent {
function read($visible = array('field_name'))
{
$users = User::all();
return $users->get()->each(function($row) use ($visible) {
$row->setVisible($visible);
});
}
}
为了在 Eagerly Loaded Models 上动态设置 $visible 属性,除了让示例 2 工作之外,我没有看到其他解决方案.但是如何?
In order to set the $visible property dynamically on Eagerly Loaded Models, I don't see another solution than to get Example 2 to work. But how?
推荐答案
由于 $visible
是在实例级别设置的(即它不是在同一类型的所有模型之间共享的静态变量),不 - 没有更好的方法可以做到这一点.
As $visible
is set on an instance level (i.e. it's not a static variable shared between all models of the same type), no - there's no better way to do this.
这篇关于Laravel/Eloquent - 急切加载隐藏/可见属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel/Eloquent - 急切加载隐藏/可见属性


- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01