Search code examples
phplaravellaravel-5.4laravel-artisan

Laravel 5.4 : Creating 'Profile Class instance' after creating new user


In Laravel 5.4, they hard-coded the user authentication system, So when you use artisan command 'make:auth' everything will be created for you under the hood, but the thing is that i want when my user get registered successfully i want to create a new instance of 'Profile Class' and make the table columns empty until the user fills his profile, So where can I place the code for creating user profile?


Solution

  • In the RegisterController you can override the registered function.

    This function is called directly after a user has successfully registered.

    /**
     * The user has been registered.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  mixed  $user
     * @return mixed
     */
    protected function registered(Request $request, $user)
    {
        // Create a profile here
    }
    

    Alternatively, you could also do this with model events directly on the user model

    class User extends Authenticatable
    {
    
        protected static function boot()
        {
            parent::boot();
    
            static::creating(function($user) {
                // Create profile here
            });
        }
    }