2016-05-15 108 views
0

我下面这样的回答:实施Laravel雄辩模型事件 - 检查引发的事件

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\[email protected]'); 
     $events->listen('cuotas.updated','App\Handlers\Events\[email protected]'); 
     $events->listen('cuotas.deleted','App\Handlers\Events\[email protected]'); 

    } 

} 

当我更新,删除或在模型$ Cuotas中创建一条新记录,我不能看到事件是否被解雇。

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

回答

0

一些如何,重新启动服务器后,事件开始自己启动。