Ad

OctoberCMS User.register Event Not Firing?

I am listening to rainlab.user.register event but nothing seem to be happening. I want to fetch the registered user_id and do something with it e.g save it in another db. I am using jwtauth plugin to register users. User can as well register via sociallogin plugin. I am testing user registration with postman, the registration request is successful but the event seem not be firing. How can I achieve this. This is my code inside the boot() method of my plugin.

Event::listen('eloquent.created:RainLab\User\Models\User', function($user) {

            $position= Position::where('title','staff')->first();
            $assign_position = new RankUser;
            $assign_position->user_id = $user->id;
            $assign_position->position_id= $position->id;
            $assign_position->save();

//Nothing being saved in DB after successful user registration.     

});

I have also tried this:

Event::listen('rainlab.user.register', function($user) {

            $position = Position::where('title','staff')->first();
            $assign_position = new Position;
            $assign_position->user_id = $user->id;
            $assign_position->position_id= $position->id;
            $assign_position->save();       


});

But still nothing happens.

What May doing wrong. Some guidance please.

Ad

Answer

Some events are exclusive to plugins like

Event::listen('rainlab.user.register', function($user) { //code });

this rainlab.user.registe will fire only while you are registering user with there controller only

so if you add user out of its scope this event will not fire

As I suspect jwtauth plugin is creating user by its own using RainLab\User\Models\User model directly so this event will not fire.

however your eloquent.created event should be fired in this case but I find some problem in you event string as in code base

static::$dispatcher->{$method}("eloquent.{$event}: ".static::class, $this);
//there is one space, my guess is this is causing ^ you problem

so try to use event string with space like blow

Event::listen('eloquent.created: RainLab\User\Models\User', function($user) 
// try to add single space here ^ it should work
{ 
    //code 
});

if it didn't work please comment.

Ad
source: stackoverflow.com
Ad