Overview
Panels are the top-level container in Filament, allowing you to build feature-rich admin panels that include pages, resources, forms, tables, notifications, actions, infolists, and widgets. All Panels include a default dashboard that can include widgets with statistics, charts, tables, and more.Prerequisites
Before using Filament, you should be familiar with Laravel. Filament builds upon many core Laravel concepts, especially database migrations and Eloquent ORM. If you’re new to Laravel or need a refresher, we highly recommend completing the Laravel Bootcamp, which covers the fundamentals of building Laravel apps.The demo project
This guide covers building a simple patient management system for a veterinary practice using Filament. It will support adding new patients (cats, dogs, or rabbits), assigning them to an owner, and recording which treatments they received. The system will have a dashboard with statistics about the types of patients and a chart showing the number of treatments administered over the past year.Setting up the database and models
This project needs three models and migrations:Owner, Patient, and Treatment. Use the following artisan commands to create these:
Defining migrations
Use the following basic schemas for your database migrations:php artisan migrate.
Unguarding all models
For brevity in this guide, we will disable Laravel’s mass assignment protection. Filament only saves valid data to models so the models can be unguarded safely. To unguard all Laravel models at once, addModel::unguard() to the boot() method of app/Providers/AppServiceProvider.php:
Setting up relationships between models
Let’s set up relationships between the models. For our system, pet owners can own multiple pets (patients), and patients can have many treatments:Introducing resources
In Filament, resources are static classes used to build CRUD interfaces for your Eloquent models. They describe how administrators can interact with data from your panel using tables and forms. Since patients (pets) are the core entity in this system, let’s start by creating a patient resource that enables us to build pages for creating, viewing, updating, and deleting patients. Use the following artisan command to create a new Filament resource for thePatient model:
app/Filament/Resources directory:
/admin/patients in your browser and observe a new link called “Patients” in the navigation. Clicking the link will display an empty table. Let’s add a form to create new patients.
Setting up the resource form
If you open thePatientResource.php file, there’s a form() method with an empty schema([...]) array. Adding form fields to this schema will build a form that can be used to create and edit new patients.
”Name” text input
Filament bundles a large selection of form fields. Let’s start with a simple text input field:/admin/patients/create (or click the “New Patient” button) and observe that a form field for the patient’s name was added.
Since this field is required in the database and has a maximum length of 255 characters, let’s add two validation rules to the name field:
”Type” select
Let’s add a second field for the type of patient: a choice between a cat, dog, or rabbit. Since there’s a fixed set of options to choose from, a select field works well:options() method of the Select field accepts an array of options for the user to choose from. The array keys should match the database, and the values are used as the form labels. Feel free to add as many animals to this array as you wish.
Since this field is also required in the database, let’s add the required() validation rule:
“Date of birth” picker
Let’s add a date picker field for thedate_of_birth column along with the validation (the date of birth is required and the date should be no later than the current day).
”Owner” select
We should also add an owner when creating a new patient. Since we added aBelongsTo relationship in the Patient model (associating it to the related Owner model), we can use the relationship() method from the select field to load a list of owners to choose from:
relationship() method is the name of the function that defines the relationship in the model (used by Filament to load the select options) — in this case, owner. The second argument is the column name to use from the related table — in this case, name.
Let’s also make the owner field required, searchable(), and preload() the first 50 owners into the searchable list (in case the list is long):
Creating new owners without leaving the page
Currently, there are no owners in our database. Instead of creating a separate Filament owner resource, let’s give users an easier way to add owners via a modal form (accessible as a+ button next to the select). Use the createOptionForm() method to embed a modal form with TextInput fields for the owner’s name, email address, and phone number:
label()overrides the auto-generated label for each field. In this case, we want theEmaillabel to beEmail address, and thePhonelabel to bePhone number.email()ensures that only valid email addresses can be input into the field. It also changes the keyboard layout on mobile devices.tel()ensures that only valid phone numbers can be input into the field. It also changes the keyboard layout on mobile devices.
Setting up the patients table
Visit the/admin/patients page again. If you have created a patient, there should be one empty row in the table — with an edit button. Let’s add some columns to the table, so we can view the actual patient data.
Open the PatientResource.php file. You should see a table() method with an empty columns([...]) array. You can use this array to add columns to the patients table.
Adding text columns
Filament bundles a large selection of table columns. Let’s use a simple text column for all the fields in thepatients table:
Filament uses dot notation to eager-load related data. We used owner.name in our table to display a list of owner names instead of less informational ID numbers. You could also add columns for the owner’s email address and phone number.
Making columns searchable
The ability to search for patients directly in the table would be helpful as a veterinary practice grows. You can make columns searchable by chaining thesearchable() method to the column. Let’s make the patient’s name and owner’s name searchable.
Making the columns sortable
To make thepatients table sortable by age, add the sortable() method to the date_of_birth column:
Filtering the table by patient type
Although you can make thetype field searchable, making it filterable is a much better user experience.
Filament tables can have filters, which are components that reduce the number of records in a table by adding a scope to the Eloquent query. Filters can even contain custom form components, making them a potent tool for building interfaces.
Filament includes a prebuilt SelectFilter that you can add to the table’s filters():
Introducing relation managers
Currently, patients can be associated with their owners in our system. But what happens if we want a third level? Patients come to the vet practice for treatment, and the system should be able to record these treatments and associate them with a patient. One option is to create a newTreatmentResource with a select field to associate treatments with a patient. However, managing treatments separately from the rest of the patient information is cumbersome for the user. Filament uses “relation managers” to solve this problem.
Relation managers are tables that display related records for an existing resource on the edit screen for the parent resource. For example, in our project, you could view and manage a patient’s treatments directly below their edit form.
You can also use Filament “actions” to open a modal form to create, edit, and delete treatments directly from the patient’s table.Use the
make:filament-relation-manager artisan command to quickly create a relation manager, connecting the patient resource to the related treatments:
PatientResourceis the name of the resource class for the owner model. Since treatments belong to patients, the treatments should be displayed on the Edit Patient page.treatmentsis the name of the relationship in the Patient model we created earlier.descriptionis the column to display from the treatments table.
PatientResource/RelationManagers/TreatmentsRelationManager.php file. You must register the new relation manager in the getRelations() method of the PatientResource:
TreatmentsRelationManager.php file contains a class that is prepopulated with a form and table using the parameters from the make:filament-relation-manager artisan command. You can customize the fields and columns in the relation manager similar to how you would in a resource:
Setting up the treatment form
By default, text fields only span half the width of the form. Since thedescription field might contain a lot of information, add a columnSpan('full') method to make the field span the entire width of the modal form:
notes field, which can be used to add more details about the treatment. We can use a textarea field with a columnSpan('full'):
Configuring the price field
Let’s add a price field for the treatment. We can use a text input with some customizations to make it suitable for currency input. It should be numeric(), which adds validation and changes the keyboard layout on mobile devices. Add your preferred currency prefix using the prefix() method; for example, prefix('€') will add a € before the input without impacting the saved output value:
Casting the price to an integer
Filament stores currency values as integers (not floats) to avoid rounding and precision issues — a widely-accepted approach in the Laravel community. However, this requires creating a cast in Laravel that transforms the integer into a float when retrieved and back to an integer when stored in the database. Use the following artisan command to create the cast:app/Casts/MoneyCast.php file, update the get() and set() methods:
MoneyCast to the price attribute in the Treatment model:
Setting up the treatments table
When the relation manager was generated previously, thedescription text column was automatically added. Let’s also add a sortable() column for the price with a currency prefix. Use the Filament money() method to format the price column as money — in this case for EUR (€):
created_at timestamp. Use the dateTime() method to display the date-time in a human-readable format:
You can pass any valid PHP date formatting string to thedateTime()method (e.g.dateTime('m-d-Y h:i A')).
Introducing widgets
Filament widgets are components that display information on your dashboard, especially statistics. Widgets are typically added to the default Dashboard of the panel, but you can add them to any page, including resource pages. Filament includes built-in widgets like the stats widget, to render important statistics in a simple overview; chart widget, which can render an interactive chart; and table widget, which allows you to easily embed the Table Builder. Let’s add a stats widget to our default dashboard page that includes a stat for each type of patient and a chart to visualize treatments administered over time.Creating a stats widget
Create a stats widget to render patient types using the following artisan command:app/Filament/Widgets/PatientTypeOverview.php file. Open it, and return Stat instances from the getStats() method:
Creating a chart widget
Let’s add a chart to the dashboard to visualize the number of treatments administered over time. Use the following artisan command to create a new chart widget:app/Filament/Widgets/TreatmentsChart.php and set the $heading of the chart to “Treatments”.
The getData() method returns an array of datasets and labels. Each dataset is a labeled array of points to plot on the chart, and each label is a string. This structure is identical to the Chart.js library, which Filament uses to render charts.
To populate chart data from an Eloquent model, Filament recommends that you install the flowframe/laravel-trend package:
getData() to display the number of treatments per month for the past year:
You can customize your dashboard page to change the grid and how many widgets are displayed.
Next steps with the Panel Builder
Congratulations! Now that you know how to build a basic Filament application, here are some suggestions for further learning:- Create custom pages in the panel that don’t belong to resources.
- Learn more about adding action buttons to pages and resources, with modals to collect user input or for confirmation.
- Explore the available fields to collect input from your users.
- Check out the list of form layout components.
- Discover how to build complex, responsive table layouts without touching CSS.
- Add summaries to your tables
- Write automated tests for your panel using our suite of helper methods.