Temporary property for Laravel Eloquent model

后端 未结 3 2030
北荒
北荒 2021-02-18 16:13

I have a Laravel Eloquent model User, which has a table with username and email columns. I need to add a property for the model on runtime, something like $user->secure. This pr

3条回答
  •  北荒
    北荒 (楼主)
    2021-02-18 17:14

    Just add an attribute to your class.

    class User extends Eloquent {
      public $secure;
    
      // ...
    }
    

    Note that it is better to declare it protected and add corresponding accessors and mutators (getSecure and setSecure methods) to your model.

提交回复
热议问题