Custom Page

I want to create a custom page where a user can execute a specific sql query, I created the custom page and linked it so from user resource they can get to it from head actions. The user is :
http://localhost/admin/users/3/votables
http://localhost/admin/users/3/votables
However I cant find any documentation on how to execute a specific sql query on my custom page or show the results using table builder.
<?php

namespace App\Filament\Resources\UserResource\Pages;

use App\Filament\Resources\UserResource;
use Filament\Resources\Pages\Page;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;

class ViewVotables extends Page
{
protected static string $resource = UserResource::class;

protected static string $view = 'filament.resources.user-resource.pages.view-votables';

public function table(Table $table): Table
{
dd('here');
}
}
<?php

namespace App\Filament\Resources\UserResource\Pages;

use App\Filament\Resources\UserResource;
use Filament\Resources\Pages\Page;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;

class ViewVotables extends Page
{
protected static string $resource = UserResource::class;

protected static string $view = 'filament.resources.user-resource.pages.view-votables';

public function table(Table $table): Table
{
dd('here');
}
}
18 Replies
Patrick Boivin
Patrick Boivin15mo ago
I think this could be a bit complicated, because the table expects a query in the context of a specific Eloquent model. I'm not sure how it could support an arbitrary SQL query, with ad-hoc columns, etc. A good place to start would be to have a textarea for the SQL query and simply display the results in an HTML table.
krekas
krekas15mo ago
It's a custom page so maybe query could be run in the mount method. It depends what's the result and query is
datarecall
datarecallOP15mo ago
@Patrick Boivin The query would be coded into the page sorry not given by the user and then change by $this->record, just trying to figure out how to run a query on the page and show the results in the table basically
Patrick Boivin
Patrick Boivin15mo ago
Ah I see
datarecall
datarecallOP15mo ago
so the query would be something like
$s = new SubjectService();
$results = $s->getVotableSubjectsFor($this->record);
//this will have all the results
//display in table
$s = new SubjectService();
$results = $s->getVotableSubjectsFor($this->record);
//this will have all the results
//display in table
datarecall
datarecallOP15mo ago
I use a service for getting the query since its pretty complex is there a way I can use that in table builder?
Patrick Boivin
Patrick Boivin15mo ago
What does the service return to you?
datarecall
datarecallOP15mo ago
it returns a collections of Subjects that a user owns
Patrick Boivin
Patrick Boivin15mo ago
Ah ok, so basically you can't use that with a Filament table, it's designed to work with a query builder The workaround is to use Sushi, if you want to use a collection with a table
Patrick Boivin
Patrick Boivin15mo ago
Filament
How to consume an external API with Filament Tables by Leandro Ferr...
A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
datarecall
datarecallOP15mo ago
ok ill give that a go but just so I am clear for making custom pages
<?php

namespace App\Filament\Resources\UserResource\Pages;

use App\Filament\Resources\UserResource;
use App\Models\User;
use Filament\Resources\Pages\Page;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

class ViewVotables extends Page
{
protected static string $resource = UserResource::class;

protected static string $view = 'filament.resources.user-resource.pages.view-votables';

public User $user;

public function mount(User $record)
{
$this->user = $record;
}

public function table(Table $table): Table
{
return $table
->query(User::query())
->columns([
TextColumn::make('name'),
])
->filters([
// ...
])
->actions([
// ...
])
->bulkActions([
// ...
]);
}
}
<?php

namespace App\Filament\Resources\UserResource\Pages;

use App\Filament\Resources\UserResource;
use App\Models\User;
use Filament\Resources\Pages\Page;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

class ViewVotables extends Page
{
protected static string $resource = UserResource::class;

protected static string $view = 'filament.resources.user-resource.pages.view-votables';

public User $user;

public function mount(User $record)
{
$this->user = $record;
}

public function table(Table $table): Table
{
return $table
->query(User::query())
->columns([
TextColumn::make('name'),
])
->filters([
// ...
])
->actions([
// ...
])
->bulkActions([
// ...
]);
}
}
Do you need the render method since its already rendering the page or does the render method just load the table inside the parent template ?
Patrick Boivin
Patrick Boivin15mo ago
You'll need to add {{ $this->table }} in the Blade view
datarecall
datarecallOP15mo ago
would that be in the view-votables.blade.php or would you make a custom make:livewire-table ?
<x-filament-panels::page>
<div>
{{ $this->table }}
</div>
</x-filament-panels::page>
<x-filament-panels::page>
<div>
{{ $this->table }}
</div>
</x-filament-panels::page>
gives me this error Too few arguments to function App\Filament\Resources\UserResource\Pages\ViewVotables::table(), 0 passed in /var/www/html/vendor/filament/actions/src/Concerns/InteractsWithActions.php on line 308 and exactly 1 expected
datarecall
datarecallOP15mo ago
ahhh perfect thank you @Patrick Boivin using sushi it looks like you need to create a custom model for it which is fine however how do you put a UserVotablesResource on a child of user/{record}/user-votable-resource
<?php

namespace App\Models;

use App\Services\SubjectService;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Sushi\Sushi;

class UserVotables extends Model
{
use Sushi;

public function getRows()
{
//API
$service = new SubjectService();
//need the user id of the votables we are trying to collect
$votables = $service->getVotableSubjects(Category::find(1), NEEDUSERID, false);
// dd($votables);

//filtering some attributes
return Arr::map($votables, function ($item) {
return Arr::only($item,
[
'id',
]
);
});
}
}
<?php

namespace App\Models;

use App\Services\SubjectService;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Sushi\Sushi;

class UserVotables extends Model
{
use Sushi;

public function getRows()
{
//API
$service = new SubjectService();
//need the user id of the votables we are trying to collect
$votables = $service->getVotableSubjects(Category::find(1), NEEDUSERID, false);
// dd($votables);

//filtering some attributes
return Arr::map($votables, function ($item) {
return Arr::only($item,
[
'id',
]
);
});
}
}
Patrick Boivin
Patrick Boivin15mo ago
@datarecall Maybe this can help: https://discord.com/channels/883083792112300104/1138671423586566184/1138853958501269566 The context is different but it shoud give you an idea of how to pass parameters to your query inside of getRows()
datarecall
datarecallOP15mo ago
excellent I will take a look thank you @Patrick Boivin
Want results from more Discord servers?
Add your server