I'd like to add a record to another table (location) when a user registers on my site.
I've got it working with the google registration that happens through Socialite:
// if user already found
if( $user ) {
// update the avatar and provider that might have changed
$user->update([
'avatar' => $providerUser->avatar,
'provider' => $driver,
'provider_id' => $providerUser->id,
'access_token' => $providerUser->token
]);
} else {
// create a new user
$user = User::create([
'name' => $providerUser->getName(),
'email' => $providerUser->getEmail(),
'avatar' => $providerUser->getAvatar(),
'provider' => $driver,
'provider_id' => $providerUser->getId(),
'access_token' => $providerUser->token,
// user can use reset password to create a password
'password' => ''
]);
}
### this is the bit I added ###
DB::table('locations')->insert([
'name'=>'Practice location',
'created_at'=> date('Y-m-d H:j:s'),
'updated_at'=> date('Y-m-d H:j:s'),
'user'=> $user->id,
'url'=> 'https://sites-jk.s3.eu-west-2.amazonaws.com/seeder/dartboard.png',
]);
The last block of code adds a record related to the new user in the locations table. I'm pleased I managed that. This is in UserController in app\Http\Controllers\Admin, a file I created following a tutorial.
What I'd like to achieve is the same as above but triggered when the user uses Laravel's built in registration system. I don't know where to find this file, I'd like to add that last block of code from above to it so when they sign up to my website it automatically adds a record in the location table related to the new user.
hopefully that makes sense.
I've looked through this post and this post but couldn't see anything useful for me / that I could understand.
CodePudding user response:
There are a couple of options available to you. You could override the RegisteredUserController.php file that ships with Laravel and add your code in there, but it would be both easier and neater to define an Observer (https://laravel.com/docs/8.x/eloquent#observers) on the User model.
This watches for any of the 'events' that happen to a user (when they're created, updated, or deleted, for example).
So using artisan :
php artisan make:observer UserObserver --model=User
will create (if it doesn't already exist) a folder at App\Observers and within that a file called UserObserver.php. That file will have boilerplate methods (which will be empty) for all the things you can 'observe', so you want to find the one that will look like this :
public function created(User $user)
{
//
}
You will see that your newly-created User is already being passed to the method, so you can just implement the code you've already got to create the location and populate it based on the new user.
Finally, you need to tell Laravel that the Observer exists - in App\Providers\EventServiceProvider.php there is a boot() method which will probably be empty - you need to tell it to observe the User model and when something happens to a User, to look at the newly-created Observer and see if it has to do anything as a result.
public function boot()
{
User::observe(UserObserver::class);
}