Hi everyone ! I've started using Nova for one of our new projects and encountered a problem with many to many relations having an intermediate table model.
I want to set an observer on the intermediate table model (ColorZone) to apply some extra operations during Eloquent events (creating, created, updated, etc.).
To be able to use the ColorZone observer, I have to define the intermediate table model via the using method in both colors and zones.
So this is where the issue comes up. Before adding the using method, pivot entries via Nova of either colors or zones were working as expected (create, update, delete, etc.), including the additional is_default pivot field. When adding the using method, pivot entries still work as expected except for the additional pivot field (is_default). The is_default field still shows, but it's not possible to change it. Even if I check the checkbox, it doesn't set is_default to 1.
The same problem occurs in the observer : I can update the ColorZone model, but can't manipulate the is_default field. For example, I tried (for debug purposes), to alter the created_at value inside the observer and it worked. But I can't alter the is_default field.
Now the fun part : if I test what I'm trying to achieve via Tinker, it works. I can set is_default on creation and update.
Here's the problematic relation :
// app\Zone.php
/**
* The colors that belong to this zone.
*/
public function colors()
{
return $this->belongsToMany('App\Color')->using('App\ColorZone')->withPivot('is_default');
}
// app\Color.php
/**
* The zones that belong to this color.
*/
public function zones()
{
return $this->belongsToMany('App\Zone')->using('App\ColorZone')->withPivot('is_default');
}
And here's the concerned Nova resource field declarations
// app\Nova\Zone.php
BelongsToMany::make('Available colors for this zone', 'colors', 'App\Nova\Color')
->singularLabel('Color')
->fields(function () {
return [
Boolean::make('Default color', 'is_default')
->displayUsing(function($field) {return $field;}),
];
}),
// app\Nova\Color.php
BelongsToMany::make('Zones associated to this color', 'zones', 'App\Nova\Zone')
->singularLabel('Zone')
->fields(function () {
return [
Boolean::make('Default color', 'is_default')
->displayUsing(function($field) {return $field;}),
];
}),
So, did I somehow find a Nova bug, or did I do something wrong in my setup ?