When using Laravel's Eloquent ORM, I can't seem to set the $hidden and $visible properties on my Model dynamically.
Example 1: This works:
class User extends Eloquent {
$this->visible = array('field_name');
function read()
{
return User::all();
}
}
Example 2: Setting the visible property on the Eloquent Class dynamically, doesn't work:
class User extends Eloquent {
function read($visible = array('field_name'))
{
$this->visible = $visible; // Also tried: $this->setVisible($visible);
return User::all();
}
}
Example 3: Solution that works on the Model itself, but not on eagerly loaded Models:
class User extends Eloquent {
function read($visible = array('field_name'))
{
$users = User::all();
return $users->get()->each(function($row) use ($visible) {
$row->setVisible($visible);
});
}
}
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?
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.
This is something I invent for this purpose:
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;
}
}