@laradan This only works in the intermediate model of the many-to-many relationship.
I have two questions here. Your relationship is currently one-to-many between Task and Project (hasMany and belongsTo) instead of many-to-many (belongsToMany and belongsToMany).
So you first need to figure out if you want one-to-many or many-to-many.
If you decide on many-to-many you can follow the following steps. As pointed out in the documentation you need to use a pivot model to achieve this. So something like this
class Task extends Model
{
public function projects()
{
return $this->belongsToMany(Project::class)->using('App\ProjectTask');
}
}
class Project extends Model
{
public function tasks()
{
return $this->belongsToMany(Task::class)->using('App\ProjectTask')
}
}
use Illuminate\Database\Eloquent\Relations\Pivot;
class ProjectTask extends Pivot
{
public static function boot()
{
parent::boot();
static::syncing(function ($item) {
dd('syncing event has been fired!');
});
}
}