Temporary property for Laravel Eloquent model

后端 未结 3 2032
北荒
北荒 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 16:49

    For those who still find this post (like I did), you may need to explicitly declare the property as null to prevent it from being automatically added to the attributes array, and thus being added to INSERT/UPDATE queries:

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

    Source: http://laravel.io/forum/02-10-2014-setting-transient-properties-on-eloquent-models-without-saving-to-database

提交回复
热议问题