This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

g.vanto67453
g.vanto67453

Re: Extending User plugin: https://octobercms.com/support/article/ob-10

Thanks for a great tutorial Sam. After adding fields directly to users table (2nd option), what is the best way to save those fields (in say a custom registration form)? I've added a field 'plan' to users table, and hooked into event on my own plugin:

Event::listen('rainlab.user.beforeRegister', function (&$data) {
    // ok this adds the data BUT User not saved with this extra data as it's not part of the user model
    $data['plan'] = 'MONTHLY';
});

But, of course the October\Rain\Auth\Models\User model doesn't know about this new field (and doesn't have rules for it), so it's not saving the newly passed 'plan' when Auth::register($data, $automaticActivation) is called - is there a way to extend the model to add an attribute to it dynamically (similar to how relationships can be extended)? Or is the only option here to subclass the rainlab User model and add extra rules for new field(s)?

Thanks, g

Last updated

daftspunky
daftspunky

g,

Yes, you should be able to addFillable on the model to include the field.

User::extend(function($u){ $u->addFillable('plan'); });

Hope this helps

g.vanto67453
g.vanto67453

Awesome that works, thanks!

1-3 of 3

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.