Access other field value for store at relationship form
return $form
->schema([
Group::make([
Section::make('article')
->schema([
TextInput::make('title'),
FileUpload::make('thumbnail'),
RichEditor::make('content'),
]),
Section::make('published')
->relationship('published')
->schema([
DatePicker::make('publish_date'),
Radio::make('edited_status')
->options([
'drafted' => 'drafted',
'completed' => 'completed',
'archived' => 'archived',
]),
Radio::make('publish_status')
->options([
'queue' => 'queue',
'preview' => 'preview',
'publish' => 'publish'
]),
])
->mutateRelationshipDataBeforeCreateUsing(function (array $data, Model $record): array {
$edited_history = [
'title' => $record['title'],
'content' => $record['content'],
'thumbnail' => $record['thumbnail'],
];
$data['edited_history'] = $edited_history;
$data['stakeholder_id'] = auth()->id();
return $data;
})
->mutateRelationshipDataBeforeSaveUsing(function (array $data, Model $record): array {
// can't access $data['title']
$edited_history = [
'title' => $record['title'],
'content' => $record['content'],
'thumbnail' => $record['thumbnail'],
];
// i want merge and store as json for history
$data['edited_history'] = $edited_history;
$data['stakeholder_id'] = auth()->id();
return $data;
})
]),
]);
// cannot access $data['title'] at mutateRelationshipDataBeforeSaveUsing() function
// or any approach for this?
12 Replies
Something feels very ‘off’ about this. Do you have a repo you can share? Might be easier than back and forth questions.
GitHub
GitHub - kamaludin21/filament-starting: learn filamentv3
learn filamentv3. Contribute to kamaludin21/filament-starting development by creating an account on GitHub.
@awcodes
I just uploaded it
If you dd($record->title) instead of using the array syntax what do you get? Since you’re inject the model it’s probably not an array.
This still looks and feels more complicated than it needs to be.
I've opened a github discussion, if you don't mind
if i use $record->title, it return old value, not the form value
Ok that makes sense. $data should be the current state of the form, but the relationship isn’t going to be included in data because relationships get processed separately when saving. Look at the vendor code to see how relationships are being saved / created. Might give you some insight into what you can do in your callbacks.
or, how save relation data from Pages/EditResource? so i can just throw the data
For example. Look at line 107 at https://github.com/filamentphp/filament/blob/3.x/packages/panels/src/Resources/Pages/CreateRecord.php
GitHub
filament/packages/panels/src/Resources/Pages/CreateRecord.php at 3....
A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS. - filamentphp/filament
for create, is already run, but not work at update,
you right, in updaterecord not include relationship
protected function handleRecordUpdate(Model $record, array $data): Model
{
$record->update($data);
return $record;
}
It’s also possible that you’re trying to hook into the process to early. And using the lifecycle hooks on EditResource and CreateResource would be a better place to handle your transformations.
Sorry, I don’t have a more direct answer, but haven’t had time to really study the code base and get familiar with the intricacies of why some of these things are relationships.
If it helps. You also have access to the old state in these callbacks. https://filamentphp.com/docs/3.x/forms/advanced#field-updates
ok mate, thanks