framework: Laravel 5.8 and syncing event: Call to undefined method exception
Laravel 5.8 is supposed to dispatch the syncing, attaching and detaching events (https://laravel.com/docs/5.8/releases search for Intermediate Table / Pivot Model Events section).
I tried it out but the following code throws the exception:
Call to undefined method App\ProjectUser::syncing()
class User extends Model
{
public function projects()
{
return $this->belongsToMany(\App\Project::class)->using(\App\ProjectUser::class);
}
}
class Project extends Model
{
public function users()
{
return $this->belongsToMany(\App\User::class)->using(\App\ProjectUser::class);
}
}
class ProjectUser extends Pivot
{
public static function boot()
{
parent::boot();
static::syncing(function ($item) {
dd('syncing event has been fired!');
});
}
}
// web.php
$project = \App\Project::first();
$project->users()->sync([1,2]);
I tried to move the boot method from ProjectUser
to User
and Project
but I get the same exception.
I cannot find any documentation or examples about it.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 21 (11 by maintainers)
@devcircus you are totally right! Now it is clear. Thanks to all of you!
For example the event fired with
$project->users()->sync([1,2]);
is saving so the boot method ofProjectUser
should be:Hmm you’re correct. This is confusing. I’ll ping Taylor about this since he was the one which added this section.
I think the wording of the documentation is possibly confusing here.
The way I read this, is that the standard observable events, listed here:
will be fired if they are relevant to the action taking place(attach, detach, sync, etc.).
Attach, detach, sync, etc., do not have their own event that is fired, but they will now trigger the appropriate existing event(updating, updated, etc.).
I may be wrong, but I believe that is what the comment in the docs is trying to say.