Logo

dev-resources.site

for different kinds of informations.

How to Extend a Filament Resource

Published at
4/7/2024
Categories
filament
php
resources
Author
andreiabohner
Categories
3 categories in total
filament
open
php
open
resources
open
Author
13 person written this
andreiabohner
open
How to Extend a Filament Resource

Introduction

In this article, we'll walk through how to extend a plugin resource, as well as other Filament resources.

Checking the Plugin's Configuration

Let's say we installed a plugin that registers resources, for example, the Filament Authentication Log, and we want to extend the available AuthenticationLogResource.php resource to add some customizations in our application.

To start with, we need to check whether the plugin enables us to configure its resources. We can take a look at the plugin's config file to see if we can locate a "resources" key or something similar that specifies the original resource(s):

// config/filament-authentication-log.php

'resources' => [
    'AutenticationLogResource' => \Tapp\FilamentAuthenticationLog\Resources\AuthenticationLogResource::class,
],
Enter fullscreen mode Exit fullscreen mode

And on the plugin's class (the class used to interact with a panel configuration file), we can check that the resources registered are the ones defined on the config file:

e.g.: /src/FilamentAuthenticationLogPlugin.php

public function register(Panel $panel): void
{
    $panel
        ->resources(
            config('filament-authentication-log.resources')
        );
}
Enter fullscreen mode Exit fullscreen mode

Now that we have confirmed that the plugin provides this configuration, let's extend the resource!

Extending the Plugin Resource

We are ready to write our own AuthenticationLogResource.php resource that extends the plugin's resource! We can write it in our project's Filament resource directory (app/Filament/Resources):

namespace App\Filament\Resources;

use Tapp\FilamentAuthenticationLog\Resources\AuthenticationLogResource as BaseAuthenticationLogResource;

class AuthenticationLogResource extends BaseAuthenticationLogResource
{
    // your custom code here
}
Enter fullscreen mode Exit fullscreen mode

If you haven't yet, you can now publish the plugin's configuration file so we would be able to instruct Filament to use our custom resource. Below is an example using our local App\Filament\Resources\AuthenticationLogResource class we've just written in the project's config/filament-authentication-log.php file:

'resources' => [
    'AutenticationLogResource' => \App\Filament\Resources\AuthenticationLogResource::class,
],
Enter fullscreen mode Exit fullscreen mode

Also, we can add the respective create, edit, list, and view pages (if they are used) on our local project. Here's an example for the list page:

namespace App\Filament\Resources\AuthenticationLogResource\Pages;

use Filament\Resources\Pages\ListRecords;

class ListAuthenticationLogs extends ListRecords
{
    // ...
}
Enter fullscreen mode Exit fullscreen mode

There's just one more thing left: in the project's custom resource class, we need to override the getPages() method to use our page instead of the plugin's:

use App\Filament\Resources\AuthenticationLogResource\Pages;

public static function getPages(): array
{
    return [
        'index' => Pages\ListAuthenticationLogs::route('/'),
    ];
}
Enter fullscreen mode Exit fullscreen mode

Great! Our new resource class should now be used.

This same technique can be applied to extend other Filament resources as well.

Feel free to ping me here or on my social networks if you have any questions, suggestions, or are using this technique. I'd love to hear from you! :)

Happy coding and see you next time!

filament Article's
30 articles in total
Favicon
Multi select default selection i filament v3
Favicon
Filament: Image Color Picker component
Favicon
My First No-Code SaaS Took off - $8K in 40 days
Favicon
Add a sidebar with options in a Filament resource
Favicon
Filament: Modify login or logout response
Favicon
Filament Database Notification Sound
Favicon
Laravel Nova vs Filament: The Best Admin Panels
Favicon
Filament: Add icon to form buttons
Favicon
Filament Breezy: add new field to Personal Info component
Favicon
Filament: Delete Attachments when Deleting a Record
Favicon
Filament: Generate Resource from Existing DB Schema
Favicon
Filament Breezy: set storage disk for avatar
Favicon
Laravel Stats : Filament Charts.js pour faire les statistiques
Favicon
Filament How to redirect to list page after (create,update) using Trait
Favicon
How to solve Symfony\Component\Routing\Exception\RouteNotFoundException Route [filament.admin.resources.api-keys.index] not
Favicon
Apa itu Laravel Filament?
Favicon
Laravel Filament: get resource table data by authenticated id
Favicon
Change Default Colors in FilamentPHP
Favicon
How to Use Searchable in Laravel 10 Filament v3
Favicon
Filament: add a confirmation password field in a form
Favicon
TomatoPHP Filament Plugins
Favicon
Debug Filament Search Query
Favicon
Filament PHP Blade UI Components Visually Explained
Favicon
Filament Context Menu
Favicon
How to Extend a Filament Resource
Favicon
Linkeeper - Lesson 02 - Start playing with Filament
Favicon
Linkeeper - Lesson 01 - Installation
Favicon
Filament has SPA mode!
Favicon
Access parent component value from the Repeater in Filament
Favicon
Filament v3 - Multi-tenancy form component scoping

Featured ones: