9

I am trying to listen to model events using laravel observers .The problem is when i submit my form (update or creating new records), nothing happened at all .Do i miss something ?

app.php

  'providers' => [
        ...
         App\Providers\CasesManagerServiceProvider::class,
]

CasesManagerServiceProvider.php

class CasesManagerServiceProvider extends ServiceProvider
{

    public function boot( )
    {

        Cases::observe(CasesObserver::class);
    }

    public function register()
    {

    }

}

CasesObserver.php

class CasesObserver
{
    private $cases;

    public function __construct(Cases $cases){
        $this->cases = $cases;
  }


    public function creating(Cases $case)
    {
        dd('creating');
    }

    public function saved(Cases $case)
    {
        dd('saved');
    }

    public function updating($case)
    {
        dd('updating');
    }
    public function updated($case)
    {
        dd('updated');
    }
}

Cases.php

class Cases extends Model
{
    const UPDATED_AT = 'modified_at';

    protected $dispatchesEvents = [
    'updating' => CasesObserver::class,
    'updated'  => CasesObserver::class,
    'creating' => CasesObserver::class,
    'saved'    => CasesObserver::class,
];
}
wahdan
  • 1,208
  • 3
  • 16
  • 26

7 Answers7

12

for me, the problem was registering observer in the register() method!
so when I put it in the boot() method every thing worked well! the reason is the order of running methods in service providers which are mentioned hear

hope be useful

Ali
  • 1,525
  • 1
  • 16
  • 27
4

Ok i have found my answer . All the problem was when I added use app\Observers\CasesObserver; in CasesManagerServiceProvider.php instead of use App\Observers\CasesObserver; . Yes the Camel case of App was the problem, so i changed to App and all things are working fine now.

wahdan
  • 1,208
  • 3
  • 16
  • 26
3

It seems to be a misuse of Composer and Laravel themselves.

You should inform them that you have added some files and configurations:

To autoload the files:

composer dump

To reconfigure the cache:

php artisan config:cache

Hope this help you too!

Gilberto Albino
  • 2,572
  • 8
  • 38
  • 50
  • This didn't seem to answer his question but I've come across this issue numerous times and a good ol' composer dumpautoload always saves the day! – Luke Mar 29 '19 at 03:03
2

You do not need to use $dispatchesEvents in your case. You should try to remove $dispatchesEvents from model, and remove __constructor() from CasesObserver.

TarangP
  • 2,711
  • 5
  • 20
  • 41
Nikita
  • 408
  • 3
  • 9
  • I tried your solution but i got the same result , nothing happened – wahdan Feb 01 '18 at 14:23
  • Do you try to dd() on boot() method in service? Before Cases::observe() call; – Nikita Feb 01 '18 at 14:26
  • You can't name a model with (case) because its a reversed keyword in php , i will try another model – wahdan Feb 01 '18 at 14:30
  • Are your laravel.log empty? With this case of course. – Nikita Feb 01 '18 at 14:36
  • Do you tried to log in the methods of observer instead dd() I mean `\Log::info('bla-bla');` – Nikita Feb 01 '18 at 14:49
  • I reproduce your case on my application. Everything works fine. Check what all of the classes which you use are in use section. I create simple Observer class with one saved() method and added to AppServiceProvider my `Model::observe(Observer::class);` – Nikita Feb 01 '18 at 19:07
  • yes i have tried your solution and it worked for me in a new project , i don't know till now how to fix this in the current project – wahdan Feb 01 '18 at 21:32
2

Not possible according to the documentation. When issuing a mass update or delete query via Eloquent.

look at the picture

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/30802854) – fatih Jan 17 '22 at 11:29
1

The reason is that you have to add a HasEvents trait to your model

<?php

use Illuminate\Database\Eloquent\Concerns\HasEvents;

class MyModel extends Model
{
    use HasEvents;

    //your code goes here
}
Igor
  • 755
  • 1
  • 10
  • 22
0

In Laravel 9.x, Once you have all your observers ready and created in the eventServiceProvider, you must validate the way you're updating the model.

Remember, mass updates, as described in the official Laravel documentation, won't trigger the Eloquent model events like 'saving', 'saved', 'updating', and 'updated'. For instance:

Cases::where('id', $id)->update(['field', 'new-value']);
// This type of mass update will not trigger model events 
//Observer will be ignored.

As a workaround, you can instead retrieve the instance first and then update the field manually. This method will fire the relevant Eloquent model events. Here's an example of how you can accomplish this:

$case = Cases::find($id);
$case->field = 'new-value';
$case->save(); // This will trigger the observer events

Note: This could be a cause. After trying several times, I had to read the documentation to realize why it wasn't executing.

felixmpa
  • 2,028
  • 1
  • 15
  • 16