Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ilex01's avatar

I want to run a query after registration

How do I run a query builder after registrating (and not logging in)?

0 likes
6 replies
jlrdw's avatar

Which scaffolding did you use?

In Breeze for example you can call the class and method in the RegisteredUserController. Work out similar if fortify or manual.

1 like
ilex01's avatar

In RegisterController.php, that's what I have and it doens't work. No new line is inserted in the table friends when someone registers.

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\Models\User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);

        DB::table('friends')->insert([
            'from_user_id' => 1,
            'to_user_id' => Auth::user()->id
        ]);
    }
ilex01's avatar

Like that it almost works:

    protected function create(array $data)
    {

        DB::table('friends')->insert([
            'from_user_id' => 1,
            'to_user_id' => 2 // 'to_user_id' => Auth::user()->id
        ]);

        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);

    }
ilex01's avatar

I cannot use: 'to_user_id' => Auth::user()->id because the user is not created

ilex01's avatar
ilex01
OP
Best Answer
Level 5

Like that, it works:

    protected function create(array $data)
    {

        $user =  User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);

        DB::table('friends')->insert([
            'from_user_id' => 1,
            'to_user_id' => $user->id,
            'accepted' => 1
        ]);

        return($user);

    }
1 like

Please or to participate in this conversation.