pmx's avatar
Level 2

Cannot make eloquent observer fire for specific model

I've been pulling my hair out with this issue and I've finally given up and i'm asking for help here!

I have observers working perfectly on my Customer model but they just refuse to work on my Orders model.

It's not throwing any errors, there is nothing in the log file either. It's just not running the observer.

I've cleared the cache, dumped the autoload file, and banged my desk with my fist but to no avail!

Anyone know whats happening here? I'd really appreciate any advice!

Some Code:

AppServiceProvider.php

...
    public function boot()
    {
        Customer::observe(CustomerObserver::class);
        Order::observe(OrderObserver::class);
        User::observe(UserObserver::class);
        Schema::defaultStringLength(191);
        
    }
...

OrderObserver.php

<?php

namespace App\Observers;

use App\Order;
use \Cache;
use \Carbon\Carbon;

class OrderObserver
{

    public function saving(Order $order) {
        // $order->searchable_name = $order->FullName;
    }

    public function saved(Order $order) {
        Cache::forever('updates.orders',Carbon::now()->timestamp);
    }
    public function created(Order $order) {
        Cache::forever('updates.orders',Carbon::now()->timestamp);
        $order->user->notify( new \App\Notifications\EmployeeOrderCreated( $order ));
    }
    public function deleted() {
        Cache::forever('updates.orders',Carbon::now()->timestamp);
    }
}
0 likes
5 replies
burlresearch's avatar

On your Order Model, what does your protected $dispatchesEvents = [] look like?

pmx's avatar
Level 2

Thanks for the reply!

I've not added that to either the Order or Customer model..

burlresearch's avatar

Maybe you don't need it since you're using the Observers ... they may be a functional replacement for that.

You could try tying right into the boot() method your Order model to save yourself from pulling your hair out:

class Order extends Model {
    protected static function boot() {
        parent::boot();
        static::saving(function (Order $order) {
            \Log::alert(__METHOD__, $order->toArray());
        });
    }
    //...
}

See if that at least barks in logs?

36864's avatar

Other than your app cache and composer autoload, this may also be an issue with OpCache. Make sure you clear it if you're using it.

If none of that works, try following these steps:

  • deleting the files in question (Order and OrderObserver)
  • making sure the app notices the files are missing (by trying to open a tinker session and observing the exceptions being thrown)
  • creating new files with the same names
  • pasting the content of the old files into the new files.

I know it sounds odd and it looks like an exercise in futility, but I've had this happen to me twice last year on different projects and that's how I fixed it then. I don't know why or how, but it can't hurt to try, right?

pmx's avatar
Level 2

@burlresearch Thanks for that - that approach works so I'm just going to stick the code in the model and be done with it.

It's not the cleanest solution but I don't want to spend any more time working it.

Thanks for your help on this, much appreciated!

Please or to participate in this conversation.