is it possible to open a relationship in a modal?

I have a model (property) that is the child in three one to many relationships. I want users to be able to edit the parent record for each of those relationships without having to navigate away from the property. The relationship is currently being set via a select.
Originally I created a hidden subform that was displayed when the select had a value but other than refreshing the entire page I couldn't workout how to populate the data in that subform. Then I tried using a action form via the header actions in the section containing the select and that sort of works but because it is being loaded from what is available in the $record I can't see how to get all of the relation stuff (images, tags, etc.) into the modal.
18 Replies
bardolf_6969
bardolf_69692mo ago
Really, no one has any suggestions, ideas, thoughts, about how this may be accomplished?
Dennis Koch
Dennis Koch2mo ago
The relationship is currently being set via a select.
And what's the issue with that? I don't think I fully understand your issue?
bardolf_6969
bardolf_69692mo ago
I want the user to be able to edit the selected record without having to navigate away from the page.
Dennis Koch
Dennis Koch2mo ago
What kind of page? If it's an EditPage and you have Selects they don't have to navigate away?
bardolf_6969
bardolf_69692mo ago
the select lets them choose the related record but the user wants to be able to edit the related record as well as I said I had a section that contained all of the related record fields via relationship but I don't see anyway to have that section refreshed after the relationship is selected. I then thought about opening the related record in a modal via a action form, but I can see how to use a relationship in an action form
bardolf_6969
bardolf_69692mo ago
yes I already tried this but it doesn't accept a relationship
Dennis Koch
Dennis Koch2mo ago
Please share some stuff what you tried and where you encountered issues
bardolf_6969
bardolf_69692mo ago
so I don't have access to all the stuff on the related model
Dennis Koch
Dennis Koch2mo ago
So you want to edit the relation of the relation?!
bardolf_6969
bardolf_69692mo ago
I want to be able to edit the related model
Dennis Koch
Dennis Koch2mo ago
Please share some code, screenshots, …
bardolf_6969
bardolf_69692mo ago
so the main model is property, that is a child in one to many relationships (tenant, owner, building) I want to find a way to be able to make updates to the parent tenant, owner, building models from the property form this is the owner section that I tried first
Dennis Koch
Dennis Koch2mo ago
Yes. That's exactly what the editOptionForm()/editOptionAction() is for. So please share what you tried and what issues you had
bardolf_6969
bardolf_69692mo ago
Forms\Components\Section::make('Owner Information')
->schema([
Forms\Components\Select::make('owner_id')
->columnSpan('full')
->label('Owner')
->options(User::all()->pluck('name', 'id'))
->searchable()
->live()
->createOptionForm([
Forms\Components\TextInput::make('name')
->label('Full Name')
->required(),
Forms\Components\Section::make('Contact')
->description('The email will be required if the owner wants to access the platform.')
->schema([
Forms\Components\TextInput::make('email')
->email(),
Forms\Components\TextInput::make('phone')
->mask('(999) 999-9999')
->tel(),
]),
Forms\Components\Section::make('Social Media')
->columns(['lg' => 3])
->schema([
Forms\Components\TextInput::make('line_id')
->maxLength(255),
Forms\Components\TextInput::make('facebook')
->maxLength(255),
Forms\Components\TextInput::make('whatsapp')
->maxLength(255),
]),
])
->createOptionUsing(function (array $data): int {
$user = new User;
$user->name = $data['name'];
$user->email = $data['email'] ?? '';
$user->phone = $data['phone'] ?? '';
$user->line_id = $data['line_id'] ?? '';
$user->facebook = $data['facebook'] ?? '';
$user->whatsapp = $data['whatsapp'] ?? '';
$user->password = Hash::make(Str::random(8));
$user->save();
$user->assignRole('owner');

return $user->id;
}),
Forms\Components\Section::make('Details')
->hidden(fn (Get $get) => empty($get('owner_id')))
->columnSpan('full')
->hiddenOn('create')
->disabledOn('create')
->relationship(
'owner',
condition: fn (?array $state): bool => filled($state['name']),
)
->schema([
Forms\Components\Section::make('Basic Information')
->schema([
Forms\Components\TextInput::make('name')
->label('Full Name')
->columnSpan('full')
->maxLength(255),
Forms\Components\TextInput::make('phone')
->mask('(999) 999-9999')
->tel(),
Forms\Components\TextInput::make('email')
->email(),
]),
Forms\Components\Section::make('Social Media')
->columns(['lg' => 3])
->schema([
Forms\Components\TextInput::make('line_id')
->maxLength(255),
Forms\Components\TextInput::make('facebook')
->maxLength(255),
Forms\Components\TextInput::make('whatsapp')
->maxLength(255),
]),
Forms\Components\SpatieMediaLibraryFileUpload::make('private')
->collection('private')
->multiple(),
])
->collapsed(),
]),
Forms\Components\Section::make('Owner Information')
->schema([
Forms\Components\Select::make('owner_id')
->columnSpan('full')
->label('Owner')
->options(User::all()->pluck('name', 'id'))
->searchable()
->live()
->createOptionForm([
Forms\Components\TextInput::make('name')
->label('Full Name')
->required(),
Forms\Components\Section::make('Contact')
->description('The email will be required if the owner wants to access the platform.')
->schema([
Forms\Components\TextInput::make('email')
->email(),
Forms\Components\TextInput::make('phone')
->mask('(999) 999-9999')
->tel(),
]),
Forms\Components\Section::make('Social Media')
->columns(['lg' => 3])
->schema([
Forms\Components\TextInput::make('line_id')
->maxLength(255),
Forms\Components\TextInput::make('facebook')
->maxLength(255),
Forms\Components\TextInput::make('whatsapp')
->maxLength(255),
]),
])
->createOptionUsing(function (array $data): int {
$user = new User;
$user->name = $data['name'];
$user->email = $data['email'] ?? '';
$user->phone = $data['phone'] ?? '';
$user->line_id = $data['line_id'] ?? '';
$user->facebook = $data['facebook'] ?? '';
$user->whatsapp = $data['whatsapp'] ?? '';
$user->password = Hash::make(Str::random(8));
$user->save();
$user->assignRole('owner');

return $user->id;
}),
Forms\Components\Section::make('Details')
->hidden(fn (Get $get) => empty($get('owner_id')))
->columnSpan('full')
->hiddenOn('create')
->disabledOn('create')
->relationship(
'owner',
condition: fn (?array $state): bool => filled($state['name']),
)
->schema([
Forms\Components\Section::make('Basic Information')
->schema([
Forms\Components\TextInput::make('name')
->label('Full Name')
->columnSpan('full')
->maxLength(255),
Forms\Components\TextInput::make('phone')
->mask('(999) 999-9999')
->tel(),
Forms\Components\TextInput::make('email')
->email(),
]),
Forms\Components\Section::make('Social Media')
->columns(['lg' => 3])
->schema([
Forms\Components\TextInput::make('line_id')
->maxLength(255),
Forms\Components\TextInput::make('facebook')
->maxLength(255),
Forms\Components\TextInput::make('whatsapp')
->maxLength(255),
]),
Forms\Components\SpatieMediaLibraryFileUpload::make('private')
->collection('private')
->multiple(),
])
->collapsed(),
]),
Dennis Koch
Dennis Koch2mo ago
I don't understand what you mean by "doesn't accept a relationship". This is meant for relationships only
bardolf_6969
bardolf_69692mo ago
sorry forgot I had changed the relationship on the owner select while I was trying something ok, I see where I was making it all harder than it had to be now. By dropping the relationship I was having to manually do all the record creation and editing (which I couldn't get to work on editing)
bardolf_6969
bardolf_69692mo ago
@Dennis Koch Thank you so much for that, I had completely missed the editOption part of the select and was off trying to reinvent the wheel. Went back to basics and all is good.
Forms\Components\Section::make('Owner Information')
->schema([
Forms\Components\Select::make('owner_id')
->columnSpan('full')
->label('Owner')
->relationship('owner', 'name')
->createOptionForm(fn(Form $form) => UserResource::form($form))
->editOptionForm(fn(Form $form) => UserResource::form($form)),
]),
Forms\Components\Section::make('Owner Information')
->schema([
Forms\Components\Select::make('owner_id')
->columnSpan('full')
->label('Owner')
->relationship('owner', 'name')
->createOptionForm(fn(Form $form) => UserResource::form($form))
->editOptionForm(fn(Form $form) => UserResource::form($form)),
]),
Want results from more Discord servers?
Add your server