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

benbowler's avatar

UserRegistered event looses all data except tax_code when passed to Listener

I've created a Laravel Listener on the UserRegistered event to link up some data to the users account based on their email. I've hit a bit of a wall however. I broadly followed this Stack Overflow post and created a nice Listener pretty easily. -> https://stackoverflow.com/questions/36975310/how-to-hook-into-spark-events-on-register

However, when I pull in the User $user object in the Listeners constructor, all it contains is the tax code, even though if I debug from within the RegisterController.php file the full user object is passed to the event.

My Listener:

<?php

namespace App\Listeners\Auth;

use Illuminate\Support\Facades\Log;

// use App\Events\Laravel\Spark\Events\Auth\UserRegistered;
use Laravel\Spark\Events\Auth\UserRegistered;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Contact;
use App\Message;
use App\User;

use Illuminate\Queue\SerializesModels;

class LinkExistingMessagesAndContacts
{
    use SerializesModels;

    /**
     * The user instance.
     *
     * @var \Illuminate\Contracts\Auth\Authenticatable
     */
    public $user;

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct(User $user)
    {
        Log::info('$user at the __construct level');
        Log::info($user);
        // Create the user object
        $this->user = $user;
    }

    /**
     * Handle the event.
     *
     * @param  UserRegistered  $event
     * @return void
     */
    public function handle(UserRegistered $event)
    {
        // Link the existing messages and contacts with this email
        // This makes sure that the users Inbox and Sent messages sections are
        // correctly filled on registration

        // TODO - wait to call this event until the email is validated.
        // Users could potentially sign up with emails that aren't theres to
        // gain unauthorised access to files and messages without this verification
        $user_id = $this->user->id;
        $email = $this->user->email;

        Log::info('$this->user at the LinkExistingMessagesAndContacts level'); // ($this->user));
        Log::info($this->user);

        // Link existing Messages
        Message::where("email", $email)->update(array('user_id' => $user_id));

        // Link existing Contact
        Contact::where("email", $email)->update(array('user_id' => $user_id));

        // TODO create historic Spark notifications for each inbox and sent item

    }
}

The debug output:

[2018-08-24 09:41:02] local.INFO: $user at register controller level
[2018-08-24 09:41:02] local.INFO: {"name":"Fron","email":"[email protected]","uri":"from","last_read_announcements_at":{"date":"2018-08-24 09:41:02.651979","timezone_type":3,"timezone":"UTC"},"trial_ends_at":"2018-09-03 09:41:02","updated_at":"2018-08-24 09:41:02","created_at":"2018-08-24 09:41:02","id":22,"tax_rate":0}
[2018-08-24 09:41:02] local.INFO: $user at the __construct level
[2018-08-24 09:41:02] local.INFO: {"tax_rate":0}
[2018-08-24 09:41:02] local.INFO: $this->user at the LinkExistingMessagesAndContacts level
[2018-08-24 09:41:02] local.INFO: {"tax_rate":0}
0 likes
1 reply

Please or to participate in this conversation.