1

I want to create a first time user login to fill some details , after the user fill some details the second time he login won`t go back to the fill details page,.

So I create a event to update Last_Login_at value Null become 1 so that the user will escape this part.

Am I doing any wrong here?

Events

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class UserHaveStoreTheFormEvent
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $user;

    /**
     * Create a new event instance.
     *
     * @param $user
     */
    public function __construct($user)
    {
        $this->user = $user;
    }

}

Listener

<?php

namespace App\Listeners;
use App\Events\UserHaveStoreTheFormEvent;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class WelcomeNewUser
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle($event)
    {
        $user = auth()->user();
        $user->last_login_at = 1;
        $user->save();
    }
}

EventService

<?php

namespace App\Providers;

use App\Events\UserHaveStoreTheFormEvent;
use App\Listeners\WelcomeNewUser;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        [
            UserHaveStoreTheFormEvent::class =>[
                WelcomeNewUser::class,
            ],
        ],
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],

    ];

    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();

        //
    }
}

controller

  public function store(StoreCustomerRequest $request)
    {
        $user = Customer::create($request->all());

        event(new UserHaveStoreTheFormEvent($user));

        return redirect()->route('admin.customers.index');
    }
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
Tim Cheah
  • 11
  • 1

1 Answers1

0

I believe the problem here is EventServiceProvider class. You have defined listeners and events like this:

protected $listen = [
    [
        UserHaveStoreTheFormEvent::class =>[
            WelcomeNewUser::class,
        ],
    ],
    Registered::class => [
        SendEmailVerificationNotification::class,
    ],

];

and the should look like this:

protected $listen = [
    UserHaveStoreTheFormEvent::class =>[
            WelcomeNewUser::class,
    ],

    Registered::class => [
        SendEmailVerificationNotification::class,
    ],
];

You wrapped first event with additional array so it's quite possible that's the reason why it's not working fine.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291