How to call parent record in a nested form?

What I am trying to do: I have a AssignmentResource that is a nested resource of the ProjectResource . In my form I want to get the contacts of the project. What I did: Look at the code My issue/the error: I found a way to do it, but it gives me errors with PHPStan so I was wondering if there is a better / alternative way to do it. Code:
final class AssignmentForm
{
public static function configure(Schema $schema): Schema
{
// @phpstan-ignore-next-line
$project = $schema->getLivewire()?->parentRecord;

return $schema
->components([
Select::make('contact_id')
->label(__('Contact'))
->relationship('project.relation.contacts', 'name',
modifyQueryUsing: function ($query) use ($project) {
if (empty($project)) {
$query->whereRaw('1 = 0'); // No project available, return empty result set
return;
}

$query->where('relation_id', $project->relation_id);
}),

// ...
]);
}
}
final class AssignmentForm
{
public static function configure(Schema $schema): Schema
{
// @phpstan-ignore-next-line
$project = $schema->getLivewire()?->parentRecord;

return $schema
->components([
Select::make('contact_id')
->label(__('Contact'))
->relationship('project.relation.contacts', 'name',
modifyQueryUsing: function ($query) use ($project) {
if (empty($project)) {
$query->whereRaw('1 = 0'); // No project available, return empty result set
return;
}

$query->where('relation_id', $project->relation_id);
}),

// ...
]);
}
}
Solution:
PHPStan will give errors in Laravel natively, ensure you are using Laravels IDE Helper too. But the parentRecord is the recommended approach....
Jump to solution
2 Replies
Solution
toeknee
toeknee4w ago
PHPStan will give errors in Laravel natively, ensure you are using Laravels IDE Helper too. But the parentRecord is the recommended approach.
TDDeveloper
TDDeveloperOP4w ago
I'm using VSCode

Did you find this page helpful?