php - Globally Hiding Attributes From Array Or JSON Conversion in laravel? -
sometimes may wish limit attributes included in model's array or json form, such passwords. so, add hidden property definition model:
class user extends model { protected $hidden = ['password']; } this model specific.
is there method hide globally?
ie,i want hide deleted_at , created_by model json result.
the easiest way creating base model. this:
class basemodel extends model { protected $hidden = ['deleted_at', 'created_by']; } and models extend that:
class user extends basemodel { } note way if wanted add hidden fields specific model have specify 2 global hidden attributes:
class user extends basemodel { protected $hidden = ['deleted_at', 'created_by', 'password']; } if bothers you, merge global attributes in contructor:
class basemodel extends model { private $globalhidden = ['deleted_at', 'created_by']; public function __construct(array $attributes = array()){ $this->hidden = array_merge($this->globalhidden, $this->hidden); parent::__construct($attributes); } }
Comments
Post a Comment