Laravel / Enloft - Eager加载隐藏/可见属性 [英] Laravel/Eloquent - Eager loaded hidden/visible properties
本文介绍了Laravel / Enloft - Eager加载隐藏/可见属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当使用Laravel的雄辩的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:设置可见属性动态的动力,不起作用:
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上动态设置$ 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 / Enloft - Eager加载隐藏/可见属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文