Add `beforeAttach` and `afterAttach` hooks
There is also a fillPivotForUpdate method in nova which I haven't found a use case for it yet...
Hi, Thanks for the PR!
How exactly would this work? Are both resources receiving the fillPivot method? (one that I'm attaching / the one that's being attached to)
In other methods, we're wrapping the after callback in model event listener. Can we do that here as well? Should we wait until both the resource and the pivot are saved?
Do you have a use case for this? I personally don't use the attachments feature that much
The way it works is like this, imagine that you have the following:
class User extends Resource
{
public function fields(Request $request): array
{
return [
BelongsToMany::make('Posts'),
];
}
}
class Post extends Resource
{
use HasCallbacks;
public static function beforeAttach(Request $request, Model $model)
{
$model->something = 123; // $model here is Post
}
}
Nova does not create the 2 resources in 1 go, first, you need to create the user model, then go to the details page and then click on the "attach post" button, where you are presented with a dropdown to pick a "post", once you click save, nova will do something like $model->posts()->attach($id) that's when the before/after Attach methods would be called
---Edit
Yeah but I think it's missing a ::saved() event on the pivot to do the afterAttach... just checked the nova source code and it call save after the fillPivot method..

Okay so I build a sample locally and got all events to work essentially it's a copy/paste of existing events but with pivot append to it.. sample usage:
class User extends Resource
{
use HasCallbacks;
public static string $model = \App\Models\User::class;
public static function beforePivotSave(NovaRequest $request, Model $model, Pivot $pivot)
{
dump('before-save');
}
public static function afterPivotSave(NovaRequest $request, Model $model, Pivot $pivot)
{
dump('after-save');
}
public static function beforePivotCreate(NovaRequest $request, Model $model, Pivot $pivot)
{
dump('before-create');
}
public static function afterPivotCreate(NovaRequest $request, Model $model, Pivot $pivot)
{
dump('after-create');
}
public static function beforePivotUpdate(NovaRequest $request, Model $model, Pivot $pivot)
{
dump('before-update');
}
public static function afterPivotUpdate(NovaRequest $request, Model $model, Pivot $pivot)
{
dump('after-update');
}
public function fields(Request $request): array
{
return [
ID::make(),
BelongsToMany::make('Roles', 'roles', Role::class)
->fields(function () {
return [
Text::make('extra_column'),
];
}),
];
}
}

When you click on the attach button you are present with this screen:

Once you click attach these events are fired in order:
public static function beforePivotSave(...)
public static function beforePivotCreate(...)
public static function afterPivotCreate(...)
public static function afterPivotSave(...)
And then once you click edit:

These events are fired in order:
public static function beforePivotSave(...)
public static function beforePivotUpdate(...)