Problem with relationmanager

I've a bit of a struggle with table list/form. I've made a table list with customers, within the edit of the customer I've integrated two relation managers. - One: With customer contact persons. - Two: With notes of the customer. Now, I want to select the customer contact name in the form of Relation manager no 2. But I can't get it working, with only the contacts of this customer. I'm seeing all contacts of all customers. I've probably have to make an query select, but I'm complete out of ideas how to fix this. Who has any advice?
3 Replies
tuto1902
tuto1902β€’11mo ago
I believe you can try modifying the relationship query. And also, you can access the owner model (Customer). Something like this
// This is the form method of your relation manager class
public function form(Form $form): Form
{
// Get access to the owner model
$owner = $this->ownerRecord();

return $form
->schema([
Forms\Components\Select::make('contact_id')
->relationship(
name:'contacts',
titleAttribute:'name',
modifyQueryUsing: function (Builder $query) use ($owner) {
$query->where('customer_id', $owner->id);
}
)
]);
}
// This is the form method of your relation manager class
public function form(Form $form): Form
{
// Get access to the owner model
$owner = $this->ownerRecord();

return $form
->schema([
Forms\Components\Select::make('contact_id')
->relationship(
name:'contacts',
titleAttribute:'name',
modifyQueryUsing: function (Builder $query) use ($owner) {
$query->where('customer_id', $owner->id);
}
)
]);
}
Virgil
Virgilβ€’11mo ago
I've solved today also 😁 . But forgot to mention here.. It's like
public function form(Form $form: Form
{
return $form
->schema([
Forms\Components\Select::make('customercontacts_id')
->label('Contactname')
->columnSpan(2)
->relationship(
name: 'customercontacts',
titleAttribute: 'name',
modifyQueryUsing: fn (Builder $query) => $query->where('customer_id', $this->ownerRecord->id ),
)
->preload()
->optionsLimit(5)
->searchable(),
]);
}
public function form(Form $form: Form
{
return $form
->schema([
Forms\Components\Select::make('customercontacts_id')
->label('Contactname')
->columnSpan(2)
->relationship(
name: 'customercontacts',
titleAttribute: 'name',
modifyQueryUsing: fn (Builder $query) => $query->where('customer_id', $this->ownerRecord->id ),
)
->preload()
->optionsLimit(5)
->searchable(),
]);
}
tuto1902
tuto1902β€’11mo ago
I'm glad you got it working πŸ‘