handleRecordUpdate

Hi, I'm trying to get the relation data within the handleRecordUpdate
protected function handleRecordUpdate(Model $record, array $data): Model
{
dd($record->toArray(), $data);
$record->editOrder($data);
$this->updateProductQty($record);
return $record;
}
protected function handleRecordUpdate(Model $record, array $data): Model
{
dd($record->toArray(), $data);
$record->editOrder($data);
$this->updateProductQty($record);
return $record;
}
It's a resource page, not a modal... but I don't know how do I get the relationship data:
return Repeater::make('items')
->relationship()
->schema([
Forms\Components\Select::make('shop_product_id')
->label('Product')
->translateLabel()
->options(Product::query()->pluck('name', 'id'))
->required()
->reactive()
->afterStateUpdated(fn ($state, Forms\Set $set) => $set('unit_price', Product::find($state)?->price ?? 0))
->distinct()
->disableOptionsWhenSelectedInSiblingRepeaterItems()
->columnSpan([
'md' => 5,
])
->searchable(),
return Repeater::make('items')
->relationship()
->schema([
Forms\Components\Select::make('shop_product_id')
->label('Product')
->translateLabel()
->options(Product::query()->pluck('name', 'id'))
->required()
->reactive()
->afterStateUpdated(fn ($state, Forms\Set $set) => $set('unit_price', Product::find($state)?->price ?? 0))
->distinct()
->disableOptionsWhenSelectedInSiblingRepeaterItems()
->columnSpan([
'md' => 5,
])
->searchable(),
Can anyone help ? thanks!
17 Replies
ingmontoya
ingmontoyaOP12mo ago
up up anyone?
Tieme
Tieme12mo ago
What exactly are you trying to do and what is your ultimate goal? Does your model have the right relation? Have you read the docs : https://filamentphp.com/docs/3.x/forms/fields/repeater#integrating-with-an-eloquent-relationship What have you already tried?
ingmontoya
ingmontoyaOP12mo ago
Hi @Tieme , thanks for answer, I'm just trying to get the relationship data in the handleRecordUpdate() located in the ResourceEdit class ( in the code I posted above you can see I'm implementing the ->relationship() method The right side of the form (in the attached picture) is the relation (orderItems) called items() in my model relation, but I just can't get that data to mutate it before save
No description
ingmontoya
ingmontoyaOP12mo ago
does that make sense ?
Tieme
Tieme12mo ago
Can you show me the output of dd($record->toArray(), $data); ?
ingmontoya
ingmontoyaOP12mo ago
No description
ingmontoya
ingmontoyaOP12mo ago
is just not showig the data in the repeater :/
Tieme
Tieme12mo ago
Can you add below code, and show me the output I'm curios if the relation data is in array before save
protected function mutateFormDataBeforeSave(array $data): array
{
dd($data);
return $data;
}
protected function mutateFormDataBeforeSave(array $data): array
{
dd($data);
return $data;
}
ingmontoya
ingmontoyaOP12mo ago
No description
Tieme
Tieme12mo ago
Can you show me the complete resource via gist : https://gist.github.com/
Dan Harrin
Dan Harrin12mo ago
why do you need the data there if it gets saved for you anyway? you can try ->dehydrated() on that component, but please think about if you really need it
ingmontoya
ingmontoyaOP12mo ago
I need the quantities per product to be able to make some operations against the inventory, so if you edit the product and increase the quantities, then the inventory will decrease (I already did this case ) but if you decrease the quantities in the orderEdit then the inventory will increase, to be able to do that I need the new quantity and compare it to the old one
Tieme
Tieme12mo ago
Personaly for that usecase i will use a model observer. https://laravel.com/docs/10.x/eloquent#observers
ingmontoya
ingmontoyaOP12mo ago
understood, thanks for the help! but does the observer has the old and new data? or just the new?
Tieme
Tieme12mo ago
Yes
public function updated(User $user)
{
// Accessing the old value
$oldEmail = $user->getOriginal('email');

// New value is accessible directly
$newEmail = $user->email;

// Do something with the old and new values
// ...
}
public function updated(User $user)
{
// Accessing the old value
$oldEmail = $user->getOriginal('email');

// New value is accessible directly
$newEmail = $user->email;

// Do something with the old and new values
// ...
}
ingmontoya
ingmontoyaOP12mo ago
thanks!!

Did you find this page helpful?