1

我想创建第一次用户登录以填写一些详细信息,用户在第二次登录后填写一些详细信息后不会返回填写详细信息页面,。

所以我创建了一个事件来更新 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();
    }
}

事件服务

<?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');
    }
4

1 回答 1

0

我相信这里的问题是EventServiceProvider阶级。您已经定义了这样的侦听器和事件:

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

];

应该是这样的:

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

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

你用额外的数组包装了第一个事件,所以很可能这就是它不能正常工作的原因。

于 2020-01-22T18:57:46.997 回答