4
我找不到在我的事件處理程序中聆聽雄辯事件的語法。在觀察者處聆聽雄辯的模型事件
我訂閱我的觀察,像這樣
Event::subscribe('Animekyun\Handlers\Events\EloquentEventHandler');
,該觀察是自我處理,並像這樣實現的:
namespace Animekyun\Handlers\Events;
use Illuminate\Events\Dispatcher;
class EloquentEventHandler
{
public function onEpisodeSave($event) {
dd('test');
}
public function subscribe(Dispatcher $events)
{
$events->listen('eloquent.saved: episode', 'Animekyun\Handlers\Events\[email protected]');
}
}
我不知道如何傾聽任何雄辯的事件在這形成。我敢肯定有一種方法來聽一個事件沒有做這樣的事情:
User::creating(function($user)
{
if (! $user->isValid()) return false;
});
編輯: 用戶模型
<?php
use Laracasts\Presenter\PresentableTrait;
use Conner\Likeable\LikeableTrait;
class Episode extends \Eloquent
{
use PresentableTrait;
use LikeableTrait;
public static $rules = [
'show_id' => 'required',
'episode_number' => 'required',
];
// Add your validation rules here
protected $presenter = 'Animekyun\Presenters\EpisodePresenter';
// Don't forget to fill this array
protected $fillable = ['title', 'body', 'links', 'show_id', 'episode_number', 'format_id', 'created_by', 'updated_by', 'screenshots'];
public function scopeSearch($query, $search)
{
return $search;
}
public function user()
{
return $this->belongsTo('User', 'created_by');
}
public function show()
{
return $this->belongsTo('Show');
}
public function format()
{
return $this->belongsTo('Format');
}
public function rating()
{
return $this->morphMany('Rating', 'rateable');
}
public function getLinksAttribute()
{
return (array) json_decode($this->attributes['links'], true);
}
public function setLinksAttribute($value)
{
$this->attributes['links'] = json_encode($value);
}
}
什麼想法?
'episode'類是如何定義的? –
像任何其他模型一樣。它擴展了'Eloquent'類並且有一些關係方法。沒有什麼不尋常 – Ortix92
你可以編輯問題並添加類聲明嗎?我的猜測是因爲命名空間並且可能區分大小寫而不工作。 –