Retrieve Records from getTableQuery in a Custom Resource for Dynamic Table Data Display

How can I retrieve the record ID inside the getTableQuery method in a custom resource so that I can dynamically display table data based on this record? Specifically, I have the following custom action:
Tables\Actions\EditAction::make()
->url(fn (Container $record): string => ContainerResource::getUrl('listOrders', ['record' => $record]))
Tables\Actions\EditAction::make()
->url(fn (Container $record): string => ContainerResource::getUrl('listOrders', ['record' => $record]))
And I want to use the retrieved record ID in the where clause of the getTableQuery as shown below:
$query = Order::join('containers', 'orders.container_id', '=', 'containers.container_id')
->where('orders.container_id', /* I need to insert the record ID here, but how can I get it? */);
$query = Order::join('containers', 'orders.container_id', '=', 'containers.container_id')
->where('orders.container_id', /* I need to insert the record ID here, but how can I get it? */);
Note: The custom resource is for a system that manages orders and containers, and I want to list orders for a specific container based on the record ID retrieved from the URL when editing a container.
Solution:
```php class CreatePage extends CreateRecord { // ... ...
Jump to solution
4 Replies
Patrick Boivin
Patrick Boivin17mo ago
I think there are a few ways to do this, I'll give you an exemple from a recent project I did
Solution
Patrick Boivin
Patrick Boivin17mo ago
class CreatePage extends CreateRecord
{
// ...

public ?int $initialParentId = null;

public function mount(): void
{
$this->initialParentId = request()->query('parent_id') ?: null;

parent::mount();
}
}
class CreatePage extends CreateRecord
{
// ...

public ?int $initialParentId = null;

public function mount(): void
{
$this->initialParentId = request()->query('parent_id') ?: null;

parent::mount();
}
}
Patrick Boivin
Patrick Boivin17mo ago
This is a create page but you get the idea... save it to a public property on the page component, then you can retrieve it in any other method or field You'll be able to do:
->where('orders.container_id', $this->container_id);
->where('orders.container_id', $this->container_id);
Marcellin
MarcellinOP17mo ago
Works great! However i replaced the request() with the $record parameter inside the mount function because the request() was returning empty results. Thanks a lot!
Want results from more Discord servers?
Add your server