I can't find the syntax to listen to eloquent events in my event handler.
I subscribed my observer like so
Event::subscribe('Animekyun\Handlers\Events\EloquentEventHandler');
This observer is self-handling and is implemented like so:
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 don't know how to listen to any eloquent event in this form. I'm sure there is a way to listen to an event without doing things like:
User::creating(function($user)
{
if ( ! $user->isValid()) return false;
});
EDIT: the 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',
];
// 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);
}
}
any ideas?
You're listening to the wrong event. Because string comparison is case sensitive, you should be listening to the eloquent.saved: Episode
event. Note the capital E
on Episode
. The class name isn't converted to lowercase as the event is fired.
Additionally, while this doesn't apply to your particular case, it should be noted that if the class is defined under a namespace, like App
for example, you need to include that namespace as well (ie App\\Episode
).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.