d7p4x
d7p4x

Reputation: 489

How to define class fields for Eloquent model

I come to Laravel from Yii and Codeigniter. And can't find a solution how to describe class fields for Eloquent model. I want to know which variables available for this class. Problem is when I define variable public => when I try to use it, this variable is empty. If i define variable protected => when I use it all is fine, but this variable is not available for auto competition (because this var is protected).

Thanks!

class User extends Eloquent
{

...
protected $registered; // works, but not available
public $registered; // not works, always empty
var $registered; // not works, always empty
...

}

Upvotes: 1

Views: 1692

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 153150

Eloquent's attributes work with an internal attributes array and rely on the magic __get() and __set() methods. However those will never get called when you define the variables. What you can do to get auto-completion and document the attributes is just add them in a docblock comment:

/**
 * @property bool $registered is the user registered?
 */
class User extends Eloquent

In fact the @property annotation is specifically designed for magic properties:

@property shows a "magic" property variable that is found inside the class.

Upvotes: 6

Related Questions