F
Filament8mo ago
core

observer not triggered from EditAction

I have this editaction in a relationsmanager it's updating correctly the pivot attributes but not triggering an updating() observer
->using(function (Model $record, array $data): Model {

$record->pivot->update($data);

return $record;
}),
->using(function (Model $record, array $data): Model {

$record->pivot->update($data);

return $record;
}),
16 Replies
Dennis Koch
Dennis Koch8mo ago
You have an Pivot model and an observer for that one?!
core
core8mo ago
yes
class IngredientRecipe extends Pivot
{... /**
* Set the quantity_metric attribute and automatically convert it to imperial.
*/
public function setQuantityMetric($value)
{
$this->attributes['quantity_metric'] = $value;

// conversion from grams to ounces
if ($this->unit_metric === 'Gram') {
$this->attributes['quantity_imperial'] = $value * 0.035274; // Conversion logic
$this->attributes['unit_imperial'] = 'ounces';
}

}
class IngredientRecipe extends Pivot
{... /**
* Set the quantity_metric attribute and automatically convert it to imperial.
*/
public function setQuantityMetric($value)
{
$this->attributes['quantity_metric'] = $value;

// conversion from grams to ounces
if ($this->unit_metric === 'Gram') {
$this->attributes['quantity_imperial'] = $value * 0.035274; // Conversion logic
$this->attributes['unit_imperial'] = 'ounces';
}

}
class IngredientRecipeObserver
{
public function updating(IngredientRecipe $ingredientRecipe)
{
$ingredientRecipe->setQuantityMetric($ingredientRecipe->quantity_metric);
}
class IngredientRecipeObserver
{
public function updating(IngredientRecipe $ingredientRecipe)
{
$ingredientRecipe->setQuantityMetric($ingredientRecipe->quantity_metric);
}
Dennis Koch
Dennis Koch8mo ago
And you registered the Observer for the model?
core
core8mo ago
yes the observer works on save() and update() via a route but on the attachaction and editaction it isnt triggered
Dennis Koch
Dennis Koch8mo ago
Did you check, whether the ->using() method is triggered and whether the pivot is an instance of your model?
core
core8mo ago
yes the using() works and updates the ingredient_recipe pivot table
Dennis Koch
Dennis Koch8mo ago
And is the pivot an instance of your PivotModel?
core
core8mo ago
yes
App\Models\Ingredient {#1592 ▼ // app/Filament/Resources/RecipeResource/RelationManagers/IngredientsRelationManager.php:161
#connection: "mysql"
#table: "ingredients"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:20 [▼
"pivot_recipe_id" => 1
"pivot_ingredient_id" => 40
"pivot_quantity_metric" => 111
"pivot_unit_metric" => "Gram"
"pivot_quantity_imperial" => null
"pivot_unit_imperial" => null
"pivot_is_required" => "Must"
"pivot_recipe_stage_id" => 1
"id" => 40
"recipe_id" => 1
"ingredient_id" => 40
"recipe_stage_id" => 1
"quantity_metric" => 111
"unit_metric" => "Gram"
"quantity_imperial" => null
"unit_imperial" => null
"is_required" => "Must"
"name" => "aperiam"
"image" => null
"avoid" => 1
]

App\Models\Ingredient {#1592 ▼ // app/Filament/Resources/RecipeResource/RelationManagers/IngredientsRelationManager.php:161
#connection: "mysql"
#table: "ingredients"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:20 [▼
"pivot_recipe_id" => 1
"pivot_ingredient_id" => 40
"pivot_quantity_metric" => 111
"pivot_unit_metric" => "Gram"
"pivot_quantity_imperial" => null
"pivot_unit_imperial" => null
"pivot_is_required" => "Must"
"pivot_recipe_stage_id" => 1
"id" => 40
"recipe_id" => 1
"ingredient_id" => 40
"recipe_stage_id" => 1
"quantity_metric" => 111
"unit_metric" => "Gram"
"quantity_imperial" => null
"unit_imperial" => null
"is_required" => "Must"
"name" => "aperiam"
"image" => null
"avoid" => 1
]

Dennis Koch
Dennis Koch8mo ago
I mean the pivot. Not the model. dd($record->pivot) should be IngredientRecipe
core
core8mo ago
Illuminate\Database\Eloquent\Relations\Pivot {#2510 ▼ // app/Filament/Resources/RecipeResource/RelationManagers/IngredientsRelationManager.php:161
#connection: "mysql"
#table: "ingredient_recipe"
#primaryKey: "id"
#keyType: "int"
+incrementing: false
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:8 [▼
"recipe_id" => 1
"ingredient_id" => 40
"quantity_metric" => "11"
"unit_metric" => "Gram"
"quantity_imperial" => null
"unit_imperial" => null
"is_required" => "Must"
"recipe_stage_id" => 1

Illuminate\Database\Eloquent\Relations\Pivot {#2510 ▼ // app/Filament/Resources/RecipeResource/RelationManagers/IngredientsRelationManager.php:161
#connection: "mysql"
#table: "ingredient_recipe"
#primaryKey: "id"
#keyType: "int"
+incrementing: false
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:8 [▼
"recipe_id" => 1
"ingredient_id" => 40
"quantity_metric" => "11"
"unit_metric" => "Gram"
"quantity_imperial" => null
"unit_imperial" => null
"is_required" => "Must"
"recipe_stage_id" => 1

Dennis Koch
Dennis Koch8mo ago
Yeah. That’s the issue. It isn’t using your model. Not sure whether that’s configurable on the filament side.
core
core8mo ago
any work around idea?
Dennis Koch
Dennis Koch8mo ago
Can you share your Model class with the relevant relationship declaration?
core
core8mo ago
class Recipe extends Model
{ public function ingredients()
{
return $this->belongsToMany(Ingredient::class, 'ingredient_recipe', 'recipe_id', 'ingredient_id')
->withPivot('quantity_metric', 'unit_metric', 'quantity_imperial', 'unit_imperial', 'is_required', 'recipe_stage_id');
}
class Recipe extends Model
{ public function ingredients()
{
return $this->belongsToMany(Ingredient::class, 'ingredient_recipe', 'recipe_id', 'ingredient_id')
->withPivot('quantity_metric', 'unit_metric', 'quantity_imperial', 'unit_imperial', 'is_required', 'recipe_stage_id');
}
class Ingredient extends Model
{ public function recipes()
{
return $this->belongsToMany(Recipe::class, 'ingredient_recipe', 'recipe_id', 'ingredient_id')
->withPivot('quantity_metric', 'unit_metric', 'quantity_imperial', 'unit_imperial', 'is_required', 'recipe_stage_id');
}
class Ingredient extends Model
{ public function recipes()
{
return $this->belongsToMany(Recipe::class, 'ingredient_recipe', 'recipe_id', 'ingredient_id')
->withPivot('quantity_metric', 'unit_metric', 'quantity_imperial', 'unit_imperial', 'is_required', 'recipe_stage_id');
}
class IngredientRecipe extends Pivot
{ public function ingredients()
{
return $this->belongsToMany(Ingredient::class);
}
class IngredientRecipe extends Pivot
{ public function ingredients()
{
return $this->belongsToMany(Ingredient::class);
}
Dennis Koch
Dennis Koch8mo ago
Not sure how this works without Filament, but you should update the relation to use the Pivot Model: https://laravel.com/docs/master/eloquent-relationships#defining-custom-intermediate-table-models
core
core8mo ago
thanks for the tips!
Want results from more Discord servers?
Add your server
More Posts