Use another column in the url for ID

I have a separate panel for sellers and I want to get orders by their reference ('FGIBNTSC02HD') instead of their ID ('123'). Can I set this on Filament level? I know I can set it on model level but that's not what I'm looking for. I just want it for this particular panel.
Solution:
@awcodes Turns out it was way easier than I had thought initially (was probably overthinking it). I remembered you can just add the key to the route: ```php public static function getPages(): array {...
Jump to solution
4 Replies
awcodes
awcodes3mo ago
Wouldn’t this be a relationship on the model and not a panel thing. Why would the panel matter to the results of the query/relationship outside of tenancy?
GavTheDev
GavTheDev3mo ago
Sorry, I'm not sure if I follow 😅 . I'm talking about retrieving OrderResource by the reference and not the ID because I don't want people to know how many orders there are. An order is not a relationship? Maybe my question is poorly phrased?
awcodes
awcodes3mo ago
If it for a particular panel then it sounds like you need a separate resource for that panel that modifies the base query for the table. Or a callback that modifies the base query for the the records based on the panel. You can get the panel in any callback with filament()->getCurrentPanel()
Solution
GavTheDev
GavTheDev3mo ago
@awcodes Turns out it was way easier than I had thought initially (was probably overthinking it). I remembered you can just add the key to the route:
public static function getPages(): array
{
return [
'index' => Pages\ListOrders::route('/'),
'create' => Pages\CreateOrder::route('/create'),
'edit' => Pages\EditOrder::route('/{record:reference}/edit'),
'details' => Pages\ViewOrderDetails::route('/{record:reference}/details'
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListOrders::route('/'),
'create' => Pages\CreateOrder::route('/create'),
'edit' => Pages\EditOrder::route('/{record:reference}/edit'),
'details' => Pages\ViewOrderDetails::route('/{record:reference}/details'
];
}
and then override the function that resolves the record:
protected function resolveRecord(int | string $key): Model
{
$record = Order::whereReference($key)->first();

if ($record === null) {
throw (new ModelNotFoundException())->setModel($this->getModel(), [$key]);
}

return $record;
}
protected function resolveRecord(int | string $key): Model
{
$record = Order::whereReference($key)->first();

if ($record === null) {
throw (new ModelNotFoundException())->setModel($this->getModel(), [$key]);
}

return $record;
}