Событие и слушатель не функционируют? - PullRequest
1 голос
/ 22 января 2020

Я хочу создать первый пользовательский логин, чтобы заполнить некоторые детали, после того, как пользователь заполнит некоторые данные, когда он во второй раз войдет, не будет go вернуться на страницу сведений о заполнении,

Итак, я создайте событие для обновления значения Last_Login_at. Значение Null станет 1, чтобы пользователь мог избежать этой части.

Я что-то здесь не так делаю?

События

<?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;
    }

}

Слушатель

<?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();

        //
    }
}

контроллер

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

        event(new UserHaveStoreTheFormEvent($user));

        return redirect()->route('admin.customers.index');
    }

1 Ответ

0 голосов
/ 22 января 2020

Я считаю, что проблема здесь в EventServiceProvider классе. Вы определили прослушиватели и события, подобные этому:

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

];

, и они должны выглядеть следующим образом:

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

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

Вы обернули первое событие дополнительным массивом, поэтому вполне возможно, что именно поэтому не работает нормально.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...