F
Filament15mo ago
Zyel

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
awcodes
awcodes15mo ago
Something feels very ‘off’ about this. Do you have a repo you can share? Might be easier than back and forth questions.
Zyel
ZyelOP15mo ago
GitHub
GitHub - kamaludin21/filament-starting: learn filamentv3
learn filamentv3. Contribute to kamaludin21/filament-starting development by creating an account on GitHub.
Zyel
ZyelOP15mo ago
@awcodes I just uploaded it
awcodes
awcodes15mo ago
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.
Zyel
ZyelOP15mo ago
I've opened a github discussion, if you don't mind if i use $record->title, it return old value, not the form value
awcodes
awcodes15mo ago
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.
Zyel
ZyelOP15mo ago
or, how save relation data from Pages/EditResource? so i can just throw the data
awcodes
awcodes15mo ago
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
Zyel
ZyelOP15mo ago
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; }
awcodes
awcodes15mo ago
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.
awcodes
awcodes15mo ago
If it helps. You also have access to the old state in these callbacks. https://filamentphp.com/docs/3.x/forms/advanced#field-updates
Zyel
ZyelOP15mo ago
ok mate, thanks
Want results from more Discord servers?
Add your server