PHP code example of othyn / filament-api-resources
1. Go to this page and download the library: Download othyn/filament-api-resources library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespace App\Models;
use Othyn\FilamentApiResources\Models\BaseApiModel;
class User extends BaseApiModel
{
protected static string $endpoint = '/users';
protected $fillable = [
'id',
'name',
'email',
'created_at',
];
protected function makeInstance(array $data): self
{
$user = new self();
// In production, validate the API response data here
// to ensure data integrity before creating the instance
$user->fill([
'id' => $data['id'],
'name' => $data['name'],
'email' => $data['email'],
'created_at' => $data['created_at'],
]);
$user->exists = true;
return $user;
}
public static function get(int|string $id, bool $forceCacheRefresh = false): ?self
{
$instance = new self();
$response = $instance->fetchResource(
params: ['id' => $id],
cacheSeconds: 60,
forceCacheRefresh: $forceCacheRefresh
);
if (empty($response['data'])) {
return null;
}
return $instance->makeInstance($response['data']);
}
}
namespace App\Filament\Resources\UserResource\Pages;
use Othyn\FilamentApiResources\Resources\Pages\ListApiRecords;
use App\Filament\Resources\UserResource;
class ListUsers extends ListApiRecords
{
protected static string $resource = UserResource::class;
}
namespace App\Filament\Resources\UserResource\Pages;
use Othyn\FilamentApiResources\Resources\Pages\ViewApiRecord;
use App\Filament\Resources\UserResource;
class ViewUser extends ViewApiRecord
{
protected static string $resource = UserResource::class;
}
namespace App\Filament\Resources;
use App\Models\User;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
class UserResource extends Resource
{
protected static ?string $model = User::class;
protected static ?string $navigationIcon = 'heroicon-o-users';
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('id'),
Tables\Columns\TextColumn::make('name'),
Tables\Columns\TextColumn::make('email'),
Tables\Columns\TextColumn::make('created_at')
->dateTime(),
])
->actions([
Tables\Actions\ViewAction::make(),
Tables\Actions\DeleteAction::make(),
])
->paginated()
->deferLoading();
}
public static function getPages(): array
{
return [
'index' => Pages\ListUsers::route('/'),
'view' => Pages\ViewUser::route('/{record}'),
];
}
}
class User extends BaseApiModel
{
protected static string $totalKey = 'meta.total'; // For APIs that return total in meta.total
protected static string $resultsKey = 'response.users'; // For APIs that return data in response.users
// ... rest of your model
}
public function refreshData(): void
{
$this->refreshRecord(forceCacheRefresh: true);
$this->notify('Data refreshed successfully!');
}
// config/filament-api-resources.php
'pagination_params' => [
'page' => 'page', // Change to 'p' if your API uses ?p=2
'per_page' => 'per_page', // Change to 'limit' if your API uses ?limit=15
],
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
return Application::configure(basePath: dirname(__DIR__))
->withExceptions(function (Exceptions $exceptions) {
// Disable truncation completely for full error details
$exceptions->dontTruncateRequestExceptions();
// Or set a custom length (default is 100 characters)
// $exceptions->truncateRequestExceptionsAt(260);
})
->create();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.