Php Laravel/Elount-渴望加载隐藏/可见属性
当使用Laravel雄辩的ORM时,我似乎无法在模型上动态设置$hidden和$visible属性 示例1:这种方法有效:Php Laravel/Elount-渴望加载隐藏/可见属性,php,laravel,eloquent,Php,Laravel,Eloquent,当使用Laravel雄辩的ORM时,我似乎无法在模型上动态设置$hidden和$visible属性 示例1:这种方法有效: 类用户扩展了雄辩的{ $this->visible=array('field_name'); 函数read() { 返回用户::all(); } } 示例2:动态设置雄辩类的visible属性无效: 类用户扩展了雄辩的{ 函数读取($visible=array('field_name')) { $this->visible=$visible;//也尝试了:$this->s
类用户扩展了雄辩的{
$this->visible=array('field_name');
函数read()
{
返回用户::all();
}
}
示例2:动态设置雄辩类的visible属性无效:
类用户扩展了雄辩的{
函数读取($visible=array('field_name'))
{
$this->visible=$visible;//也尝试了:$this->setVisible($visible);
返回用户::all();
}
}
示例3:对模型本身有效,但对急切加载的模型无效的解决方案:
类用户扩展了雄辩的{
函数读取($visible=array('field_name'))
{
$users=User::all();
return$users->get()->每个(函数($row)使用($visible){
$row->setVisible($visible);
});
}
}
为了在急切加载的模型上动态设置$visible属性,除了让示例2工作之外,我看不到其他解决方案。但是如何设置呢?因为
$visible
是在实例级别设置的(即,它不是在同一类型的所有模型之间共享的静态变量),没有-没有更好的方法可以做到这一点。这是我为此而发明的:
use Illuminate\Database\Eloquent\Model;
/*
* trait allows to use a single method:
* getSerialized
* this function works as the following method:
* Illuminate\Database\Query\Builder::get(),
* and also returns the collection
* but accepts a parameter of an array type
* like that
* $records = Table1::getSerialized([
* 'appends' => ['calc_field1', 'calc_field2'],
* 'hidden' => ['field1', 'field2', 'field3'],
* 'visible' => ['id', 'name', 'calc_field1', 'calc_field2'],
* ]);
*
* the returned collection accords with params
* read more in Laravel documentation
* https://laravel.com/docs/5.1/eloquent-serialization
*/
trait Serialization
{
// scope filters ---------------------------------------------------------
private static $staticAppends;
private static $staticHidden;
private static $staticVisible;
public function __construct(array $attributes = []){
parent::__construct($attributes);
if (isset(self::$staticAppends)){
$this->appends = self::$staticAppends;
}
if (isset(self::$staticHidden)){
$this->hidden = self::$staticHidden;
}
if (isset(self::$staticVisible)){
$this->visible = self::$staticVisible;
}
}
public function scopeGetSerialized($query, array $params){
if (isset(self::$staticAppends)){
$staticAppends = self::$staticAppends;
} else {
$staticAppends = [];
}
if (isset(self::$staticHidden)){
$staticHidden = self::$staticHidden;
} else {
$staticHidden = [];
}
if (isset(self::$staticVisible)){
$staticVisible = self::$staticVisible;
}else {
$staticVisible = [];
}
if (isset($params['appends'])){
self::$staticAppends = $params['appends'];
}
if (isset($params['hidden'])){
self::$staticHidden = $params['hidden'];
}
if (isset($params['visible'])){
self::$staticVisible = $params['visible'];
}
$res = $query->get();
self::$staticAppends = $staticAppends;
self::$staticHidden = $staticHidden;
self::$staticVisible = $staticVisible;
return $res;
}
}
我也有同样的问题。。你能找到解决方案吗?我只是通过对模型中的方法执行->选择()来解决这个问题。在处理多个关系时,该解决方案将要求您选择关系列。否则,查询将失败。这是预期的行为,但在我的案例中不是预期的。
Project
是另一个雄辩的模型吗?您是否试图在用户和项目之间切换?@fideloper这只是一个输入错误。在我最初的回答中它是固定的。这不是另一种模式。