Reputation: 1258
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,
];
}
Upvotes: 9
Views: 32241
Reputation: 2128
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.
Upvotes: 2
Reputation: 408
You do not need to use $dispatchesEvents in your case. You should try to remove $dispatchesEvents
from model, and remove __constructor()
from CasesObserver.
Upvotes: 2
Reputation: 31
Not possible according to the documentation. When issuing a mass update or delete query via Eloquent.
Upvotes: 2
Reputation: 1202
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
}
Upvotes: 2
Reputation: 1795
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
Upvotes: 13
Reputation: 2744
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!
Upvotes: 3
Reputation: 1258
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.
Upvotes: 4