I cannot find the syntax for listening to eloquent events in the event handler.
I signed up for my observer
Event::subscribe('Animekyun\Handlers\Events\EloquentEventHandler');
This observer is self-service and is implemented as follows:
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\EloquentEventHandler@onEpisodeSave');
}
}
I do not know how to listen to any eloquent event in this form. I am sure there is a way to listen to the event without doing things like:
User::creating(function($user)
{
if ( ! $user->isValid()) return false;
});
EDIT: user model
<?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',
];
protected $presenter = 'Animekyun\Presenters\EpisodePresenter';
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);
}
}
any ideas?
source
share