F
Filament13mo ago
Jacques

Reuse resource form in another livewire component

Hi I want to know is it possible to reuse a resource form
public function form(Form $form): Form
{
return OrganisationResource::form($form)
->statePath('data')
->getClone()
->model(Organisation::class);
}
public function form(Form $form): Form
{
return OrganisationResource::form($form)
->statePath('data')
->getClone()
->model(Organisation::class);
}
I have this form on livewire component and all is showing fine but when i save the selects is not saving here is dd of data
array:10 [// app/Livewire/Organisation/Register.php:35
"logo" => "LUfEL9i1C2tIr9wIAmkMESQFqoTxgX-metabG9nby5wbmc=-.png"
"name" => "York Stanley Inc"
"district_id" => null
"contact_person" => "Graham Cote Traders"
"contact_number" => "Guzman Rosario Co"
"email" => "[email protected]"
"website" => "https://www.gigazoqug.mobi"
"councilors" => null
"votes" => null
"status" => "pending"
]
array:10 [// app/Livewire/Organisation/Register.php:35
"logo" => "LUfEL9i1C2tIr9wIAmkMESQFqoTxgX-metabG9nby5wbmc=-.png"
"name" => "York Stanley Inc"
"district_id" => null
"contact_person" => "Graham Cote Traders"
"contact_number" => "Guzman Rosario Co"
"email" => "[email protected]"
"website" => "https://www.gigazoqug.mobi"
"councilors" => null
"votes" => null
"status" => "pending"
]
All selects are null for some reason Also I have dependant select based on another select but this is also not working here but does work on original form Any ideas? Is this possible?
9 Replies
Dennis Koch
Dennis Koch13mo ago
I think model should be the actual record and not just the class
Jacques
JacquesOP13mo ago
I will give it a try thanks Dennis. I think i might just give this different approach but would be nice to figure out this
erikms61
erikms6113mo ago
Hi Jacques, I notice that your district_id, councilors and votes fields are all null; are they table relations? I have a similar problem: when I try to reuse the form, Filament breaks on table relationships.
Jacques
JacquesOP13mo ago
Yes there is relationships. I have kind of given up this way and got another way working What I mean is i went for the normal register page and added fields that way. Still want to know how to get this working but will get back to that later The votes actually is not relationship
erikms61
erikms6113mo ago
I solved it by creating the record, then redirecting the user to the edit url... ugly and hacky but it works
Jacques
JacquesOP13mo ago
but what they all have in comon is they all using selects and for some reason the selects dont pass data "solved it by creating the record" what do you mean? can you show me what you did?
erikms61
erikms6113mo ago
ok... my problem was: I have a table of locations that have stocklevels, and I wanted to have a button saying "restock". Then the Model determines how much and from where, and creates a User Activity to go get those stock items. So far so good. However, once the Activity has been created, (or new d, preferably), I wanted the user to be able to review the Activity before saving it. ...But I already had a form for than, and I don't want to duplicate that code. so this is my solution:
Action::make('move_stock')
->mountUsing(
function (LocationStock $record) {
$activity = null;
$stock = $record->getCurrentStock();
if($stock < $record->maximum) {
$activity = $record->replenish();
}
if($stock > $record->maximum) {
$activity = $record->moveOut();
}
if(!is_null($activity)) {
$activity->save();
*return redirect(route('filament.admin.resources.activities.edit', $activity->id));*
}
}
),
Action::make('move_stock')
->mountUsing(
function (LocationStock $record) {
$activity = null;
$stock = $record->getCurrentStock();
if($stock < $record->maximum) {
$activity = $record->replenish();
}
if($stock > $record->maximum) {
$activity = $record->moveOut();
}
if(!is_null($activity)) {
$activity->save();
*return redirect(route('filament.admin.resources.activities.edit', $activity->id));*
}
}
),
The thing I really hate about this is that the activity already exists, or I can't show it. I'd have preferred something like ->form(ActivityResource::getFormFields()) ...feed that the just-created object and the form would be filled
Jacques
JacquesOP13mo ago
Ah ok I see what you doing. It gives me some idea. I will test out some more and let you know for now I had to move on but I will come back to this and try and get this working because I see benefit in reusing forms and having one place for the fields
erikms61
erikms6113mo ago
I'd appreciate that; when I dig into the Filament basics, it seems that the ->relation() field is not being propagated
Want results from more Discord servers?
Add your server