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

vincentvankekerix's avatar

Create Record in other database table when creating a new User

Dear all,

In another Laravel project, without having Spark implemented, I also create a new record in the database table 'organisations' when creating a new User. I do this by using the following code in my RegisterController file:

public function create(array $data)
{
    $organisation = new Organisation;
    $organisation->profile_id = $data['profile_id'];
    $organisation->name = $data['org_name'];
    $organisation->city = $data['org_city'];
    $organisation->state = $data['org_state'];
    $organisation->country = $data['org_country'];
    $organisation->website = $data['org_website'];
    $organisation->email = $data['org_email'];
    $organisation->phone = $data['org_phone'];
    $organisation->save();

    return User::create([
        'organisation_id' => $organisation->id,
        'name' => '' . $data['name'],
        'function' => $data['function'],
        'email' => $data['email'],
        'phone' => $data['phone'],
        'mobile' => $data['mobile'],
        'password' => bcrypt($data['password']),
        'trial_ends_at' => Carbon::now()->addDays(14)
    ]);
}

Works like a charm. I tried to implement the same method in a project that does use Spark, but unfortunately, this method does not seem to work here. When registering a new user through the registration form, the form returns that 'an error has occurred'. There aren't more specific details.

I tried implementing the code above within Spark::createUsersWith in the SparkServiceProvider, but that didn't make any difference.

Who could help me back on track here?

Thanks! Vincent

0 likes
4 replies
Cronix's avatar

There is an event triggered when a user is created. You can just create a listener and do your other setup there.

Look in app/Providers/EventServiceProvider.php. The first event listed should be 'Laravel\Spark\Events\Auth\UserRegistered'

There are lots of events you can hook into.

vincentvankekerix's avatar

Thanks Cronix! At least I got it working again to create a new record in the 'organisation' table making use of a listener.

One additional question: in the listener file, I can't make it work to get data passed on from the user registration form to the listener file. I would like to populate the new organisation record with data from the user registration form, which are stored in the $data array.

How can I pass the $data array on to the Listener file?

Cronix's avatar
Cronix
Best Answer
Level 67

I don't think you can pass it directly since the event only provides the user object that was created. In your listener though, you should be able to typehint the request object in the handle() method and pull the data from the form in from there?

public function handle(UserRegistered $event, Request $request) {
    $user = $event->user;
    $someField = $request->field1;
    $anotherField = $request->field2;
    // etc
}
vincentvankekerix's avatar

You helped me out in the right direction there, got it to work!

I first passed on $request within the event 'UserRegistered.php':

public function __construct($user, $request)
{
    $this->user = $user;
    $this->request = $request;
}

Next, I used the following code in the listener 'CreateNewOrganisation':

public function handle(UserRegistered $event)
{
    // Create new Organisation
    $profile_id = $event->request->input('profile_id');
    $org_name = $event->request->input('team');
    $org_city = $event->request->input('org_city');
    $org_state = $event->request->input('org_state');
    $org_country = $event->request->input('org_country');
    $org_website = $event->request->input('org_website');
    $org_email = $event->request->input('org_email');
    $org_phone = $event->request->input('org_phone');

    $organisation = new Organisation;
    $organisation->profile_id = $profile_id;
    $organisation->name = $org_name;
    $organisation->city = $org_city;
    $organisation->state = $org_state;
    $organisation->country = $org_country;
    $organisation->website = $org_website;
    $organisation->email = $org_email;
    $organisation->phone = $org_phone;
    $organisation->save();
    $organisation_id = $organisation->id;

    // Store Organisation ID in the User Record
    User::where('id', $event->user->id)->update(['organisation_id' => $organisation_id]);
}

The last bit of code makes sure that the newly created user is also connected in the SQL-DB to the newly created organisation.

Thanks for your help, Cronix!

Please or to participate in this conversation.