【问题标题】:Implementing Laravel Eloquent Model Events - checking for fired events实现 Laravel Eloquent 模型事件 - 检查触发事件
【发布时间】:2016-09-11 17:05:15
【问题描述】:

我正在关注这个答案:

Laravel Model Events

我正在尝试实现标记答案的代码。

我认为everythink 只是作为“答案”,但它似乎并没有触发事件。

如何检查事件是否正在触发?

这是我的模型:

<?php

namespace App;

use App\Traits\ModelEventThrower;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class Cuotas extends Model
{
    use ModelEventThrower;


    protected $fillable = [ 'apartamento_id','fecha_cuota','fecha_vencimiento', 'tipo_cuota_id', 'monto', 'descripcion_cuota','saldo'  ];
    protected $dates = ['created_at', 'updated_at', 'deleted_at','fecha_vencimiento','fecha_cuota'];
    protected $appends = ['tipo_cuota'];

    //protected static $othersEvents = ['saving'];



    function getTipoCuotaAttribute(){
        $id = $this->attributes['tipo_cuota_id'];
        $tipoCuota = TipoCuota::lists('descripcion_cuota','id')->toArray();
        return $tipoCuota[$id];
    }


    function getFechaVencimientoAttribute($value){
        return Carbon::parse($value)->format('d-m-Y');
    }
    function setFechaVencimientoAttribute($value){
        $this->attributes['fecha_vencimiento'] = Carbon::createFromFormat('d-m-Y', $value);
    }

    function getFechaCuotaAttribute($value){
        return Carbon::parse($value)->format('d-m-Y');
    }
    function setFechaCuotaAttribute($value){
        $this->attributes['fecha_cuota'] = Carbon::createFromFormat('d-m-Y', $value);
    }

    function apartamento()
    {
        return $this->belongsTo('App\Apartamentos', 'apartamento_id');
    }
    public function pagos(){
        return $this->hasMany('App\Pagos','cuota_id', 'id');
    }
}

这是我的特质:

<?php

namespace App\Traits;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Event;

/**
 * Class ModelEventThrower
 * @package App\Traits
 *
 *  Automatically throw Add, Update, Delete events of Model.
 */
trait ModelEventThrower {

    /**
     * Automatically boot with Model, and register Events handler.
     */
    protected static function bootModelEventThrower()
    {

        foreach (static::getModelEvents() as $eventName) {
            static::$eventName(function (Model $model) use ($eventName) {
                try {
                    $reflect = new \ReflectionClass($model);
                    Event::fire(strtolower($reflect->getShortName()).'.'.$eventName, $model);
                } catch (\Exception $e) {
                    return true;
                }
            });
        }
    }

    /**
     * Set the default events to be recorded if the $recordEvents
     * property does not exist on the model.
     *
     * @return array
     */
    protected static function getModelEvents()
    {
        if (isset(static::$othersEvents)) {
            return static::$othersEvents;
        }

        return [
            'created',
            'updated',
            'deleted',
        ];
    }
} 

我的 EventServiceProvider:

<?php

namespace App\Providers;

use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        'App\Events\SomeEvent' => [ 'App\Listeners\EventListener', ],
    ];

    /**
     * Register any other events for your application.
     *
     * @param  \Illuminate\Contracts\Events\Dispatcher  $events
     * @return void
     */
    public function boot(DispatcherContract $events)
    {
        parent::boot($events);
        $events->subscribe('App\Handlers\Events\CuotasEventHandler');
    }
}

我的事件处理程序:

<?php

namespace App\Handlers\Events;

use App\Cuotas;

class CuotasEventHandler{

    /**
     * Create the event handler.
     *
     * @return \App\Handlers\Events\CuotasEventHandler
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle cuotas.created event
     */

    public function created(Cuotas $cuotas)
    {
       dd('created');
    }

    /**
     * Handle cuotas.updated event
     */

    public function updated(Cuotas $cuotas)
    {
        //Implement logic
        dd('updated');
    }

    /**
     * Handle cuotas.deleted event
     */

    public function deleted(Cuotas $cuotas)
    {
        //Implement logic
        dd('deleted');
    }

    /**
     * @param $events
     */
    public function subscribe($events)
    {
        $events->listen('cuotas.created','App\Handlers\Events\CuotasEventHandler@created');
        $events->listen('cuotas.updated','App\Handlers\Events\CuotasEventHandler@updated');
        $events->listen('cuotas.deleted','App\Handlers\Events\CuotasEventHandler@deleted');

    }

}

当我在模型 $Cuotas 中更新、删除或创建新记录时,我看不到事件是否被触发。

我的意图是使用其他模型“pagos”中已经完成的付款来更新模型的“saldo”(余额)列。

【问题讨论】:

    标签: php laravel laravel-5 eloquent


    【解决方案1】:

    在我重新启动服务器后,事件开始自行触发。

    【讨论】:

      猜你喜欢
      • 2021-09-19
      • 2017-05-08
      • 2023-03-22
      • 2018-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多