Hamed Adil
Hamed Adil

Reputation: 483

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?

Upvotes: 0

Views: 871

Answers (2)

Omar Tarek
Omar Tarek

Reputation: 744

In your app\Http\Controllers\Auth\RegisterController.php on the create() method right after you create a new user you can do this:

use App\Profile;              // <-- Import this at the top

protected function create(array $data)
     {
         $user = User::create([    // <-- change this as you see fit
             'name' => $data['name'],
             'email' => $data['email'],
             'password' => bcrypt($data['password']),
         ]);

         Profile::create(['user_id' => $user->id]);

         return $user;
     }

Upvotes: 2

Nicklas Kevin Frank
Nicklas Kevin Frank

Reputation: 6337

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
        });
    }
}

Upvotes: 3

Related Questions