How can the resource query be modified on an Infolist page?

Hi, When loading a resource, the standard select uses *. On a table page, I can use modifyQueryUsing() to modify the resource to suit the table's needs, but I don't see a way to do that with a View page that doesn't have a modifyQueryUsing() method either on the Page or the Infolist.
4 Replies
LeandroFerreira
LeandroFerreira9mo ago
what would you like to modify in the view page?
BuddhaNature
BuddhaNatureOP9mo ago
Let's assume I have this hypothetical table DB table with these fields: id name, string title, string data, json notes, json details, json The resource:
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()->withoutGlobalScopes([SoftDeletingScope::class]);
}
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()->withoutGlobalScopes([SoftDeletingScope::class]);
}
The Table on the List Page has the following on the Table component:
->modifyQueryUsing(fn (Builder $query) => $query
->select(['id', 'name', 'title']));
->modifyQueryUsing(fn (Builder $query) => $query
->select(['id', 'name', 'title']));
This limits the scope and doesn't load any of the json data. This work now. On the view page, I'd like to modify the resource query to this:
$query->select(['id', 'name', 'title', 'data'])
$query->select(['id', 'name', 'title', 'data'])
This query includes data, but not notes and details. And then on an additional custom page, I'd like to modify the resource query to this:
$query->select(['id', 'name', 'title', 'notes', 'details'])
$query->select(['id', 'name', 'title', 'notes', 'details'])
This query includes notes and details, but not data. If a DB table with a lot of fields, especially larger fields like TEXT of JSON, I'm trying to limit the scope on a page by page basis. Was the above clear @Leandro Ferreira ?
LeandroFerreira
LeandroFerreira9mo ago
I guess you need to override the mount method in your ViewPage
public function mount(int|string $record): void
{
$this->record = //YourQuery...

$this->authorizeAccess();

if (! $this->hasInfolist()) {
$this->fillForm();
}
}
public function mount(int|string $record): void
{
$this->record = //YourQuery...

$this->authorizeAccess();

if (! $this->hasInfolist()) {
$this->fillForm();
}
}
BuddhaNature
BuddhaNatureOP9mo ago
This is the route I went with. I'd love your thoughts.
public static function getEloquentQuery(): Builder
{
$query = parent::getEloquentQuery()
->withoutGlobalScopes([SoftDeletingScope::class]);

return match (Route::currentRouteName()) {
ViewPageSampeOne::getRouteName() => $query->select(['id', 'name', 'title', 'notes']),
ViewPageSampeTwo::getRouteName() => $query->select(['id', 'name', 'metadata']),
default => $query->select(['id', 'name', 'title']),
};
}
public static function getEloquentQuery(): Builder
{
$query = parent::getEloquentQuery()
->withoutGlobalScopes([SoftDeletingScope::class]);

return match (Route::currentRouteName()) {
ViewPageSampeOne::getRouteName() => $query->select(['id', 'name', 'title', 'notes']),
ViewPageSampeTwo::getRouteName() => $query->select(['id', 'name', 'metadata']),
default => $query->select(['id', 'name', 'title']),
};
}

Did you find this page helpful?