Listen to the eloquent event of the model in the observer

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',
    ];

    // 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?

+4
source share
1 answer

You are listening to the wrong event. Since string comparisons are case sensitive, you should listen to the event eloquent.saved: Episode. Pay attention to capital Eon Episode. The class name is not converted to lowercase when the event fires.

, , , , , App, , (.. App\Episode).

+1

All Articles