【问题标题】:Dispatching an event in Laravel 5 on model created在 Laravel 5 中在创建的模型上调度事件
【发布时间】:2016-04-15 14:45:53
【问题描述】:

我想创建一个 Laravel 注册系统,一旦用户注册,我将发送一封电子邮件以验证电子邮件。我尝试向创建的方法添加分派事件,但出现错误

 Fatal error: Non-static method Illuminate\Contracts\Events\Dispatcher::fire() cannot be called statically

这是我想出的。

  <?php

namespace App;

use Laravel\Cashier\Billable;
use Laravel\Spark\Teams\CanJoinTeams;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as BaseUser;
use Laravel\Spark\Auth\TwoFactor\Authenticatable as TwoFactorAuthenticatable;
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher; 
use Laravel\Spark\Contracts\Auth\TwoFactor\Authenticatable as TwoFactorAuthenticatableContract;

class User extends BaseUser implements TwoFactorAuthenticatableContract
{
    use Billable, TwoFactorAuthenticatable,CanJoinTeams;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'email',
        'name',
        'password',
    ];

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = [

    ];


    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'card_brand',
        'card_last_four',
        'extra_billing_info',
        'password',
        'remember_token',
        'stripe_id',
        'stripe_subscription'       
    ];

    /**
     * Boot the model.
     *
     * @return void
     */
    public static function boot()
    {
        parent::boot();

        static::creating(function ($user) {
            $user->token = str_random(30);
        });

        static::created(function ( $user) {        

           EventDispatcher::fire('UserCreated');   

        });
    }


    /**
     * Confirm the user.
     *
     * @return void
     */
    public function confirmEmail()
    {
        $this->verified = true;
        $this->token = null;

        $this->save();
    }    

}

我也试过把代码改成

  use Billable, TwoFactorAuthenticatable,CanJoinTeams, EventDispatcher;

并用

替换引导部分
 static::created(function ( $user, EventDispatcher $event) {        

       $event->fire('UserCreated');   

    });

但它给了我另一个错误

App\User cannot use Illuminate\Contracts\Events\Dispatcher - it is not a trait

创建模型后如何触发事件?

【问题讨论】:

    标签: php laravel laravel-5 eloquent laravel-spark


    【解决方案1】:

    在 Laravel >5.4 及更高版本中,您可以在 Model 类中关联 eloquent 事件来触发自定义事件 - 例如'created' =&gt; ProjectCreated::class。所以基本上,在 Eloquent 模型上 created fire ProjectCreated Event。

    class Project extends Model
    {
        /**
         * @var array
         */
        protected $fillable = ['title', 'description', 'client_id'];
    
        /**
         *
         * Eloquent model events
         *
         * @var array
         */
        protected $dispatchesEvents = [
            'created' => ProjectCreated::class,
        ];
    }

    【讨论】:

      【解决方案2】:

      模型的生命周期事件已由 Eloquent 触发,因此无需自己触发。使用以下代码创建事件名称:

      $event = "eloquent.{$event}: ".get_class($model);
      

      因此,如果你想监听 created 事件,你需要监听 "eloquent.created: App\User"。事件处理程序将获得相关的 User 模型作为 handle() 参数之一。

      如果你想调度自己的事件,你可以使用 Event 外观:

      Event::fire('UserCreated', $user);
      

      你可以在这里阅读更多关于 Laravel 事件的信息:https://laravel.com/docs/5.1/events

      【讨论】:

      • 由于我对 Laravel 还很陌生并且不太了解事件生命周期,所以我尝试使用 Event::fire 方式,但出现致命错误:找不到 Class 'App\Event' Do我需要在其他地方调用它吗?
      • 添加“使用事件;”在文件顶部或调用 \Event::fire
      猜你喜欢
      • 1970-01-01
      • 2015-09-29
      • 1970-01-01
      • 2016-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-21
      • 2018-07-29
      相关资源
      最近更新 更多