php - Laravel protected attributes and Mutators -
i have problem laravel , protected $attributes , mutators.
i have user ranking points. want add user model attribution ranking position.
in user model have public function this:
public function getrankpositionattribute(){ $userpoints= ranking::where('user_id','=',$this->id)->first(); $userposition = ranking::where('points','>',$userpoints->points)->count()+1; return $userposition; }
i set:
protected $attributes = array('rankposition'='');
but it's not working (i don't see value rankposition in attributes). strange thing when add (for example) value this:
protected $attributes =array('test'=>'yes');
laravel don't see test...
but when add this:
protected $appends = array('rankposition');
and in controller find user , response json in json response see value rankposition correct value... :(
what im doing wrong? why "my laravel" skips protected $attributes?
please me.
this because if provide protected $attributes
in class laravel doesn't override when source of attributes table . here source of $attributes
database column.
but when this:
$user = new user;
then see test
attribute.
so dynamically adding attributes should use appends
property on model.
Comments
Post a Comment