Laravel Filament Form show related data from other table
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Mastering Relationships in Filament Forms: Showing Related Data Seamlessly
As developers building complex applications with Laravel and Filament, one of the most frequent hurdles we encounter is displaying related data within forms. Specifically, when working with Eloquent BelongsTo relationships, getting that related information to populate a form field often requires a slightly different approach than simply viewing it in an Infolist.
This post will walk you through the common issue you are facing—showing related data like a Salutation.name inside a Filament Form—and provide a robust, practical solution using standard Laravel and Filament practices. We'll explore why your existing attempt didn't work immediately and how to correctly leverage Eloquent relationships within the form schema.
The Challenge: Why Related Data Fails in Forms
You are running into a common point of confusion regarding how data is loaded and accessed in Filament. When you use the infolist() method, Filament often handles eager loading or relationship traversal automatically for display purposes, which is why $infolist works perfectly fine.
However, when defining the form() schema, Filament expects explicit instructions on how to retrieve the data for input. If you try to access a nested relationship directly as a simple TextInput::make('salutation.name'), Filament might not automatically know how to resolve the necessary Eloquent query required for editing or creation context, especially if the relationship isn't fully loaded or the structure isn't explicitly defined for form interaction.
The key is ensuring that the component you use (like Select or a custom component) has access to the necessary scope to perform the lookup based on the parent record being edited.
The Solution: Leveraging Eloquent Relationships in Forms
For displaying related records, especially when dealing with many-to-one relationships like Contact belonging to a Salutation, the most effective method is to use Filament's built-in components that accept relationship constraints, such as the Select component. This allows you to leverage Laravel’s Eloquent capabilities directly within the form definition.
Let's refine your approach using the Select component to dynamically populate the relationship:
Step 1: Ensuring Relationships are Loaded (Eager Loading)
Before anything else, ensure that when the Contact model is loaded for editing, the related Salutation data is available. This is crucial for performance and correctness. You should use eager loading in your resource's getRecord() or scope methods if you are fetching data manually, or rely on Filament’s default behavior which typically handles this when using standard relationship access.
Step 2: Implementing the Dynamic Select Field
Instead of attempting to pull a nested field directly, we instruct the form to select a related model based on its ID and display a friendly name.
Here is how you can structure your form() method in ContactResource.php:
use Filament\Forms;
use Filament\Forms\Components\Select;
use App\Models\Salutation; // Ensure this import is correct
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Section::make()
->schema([
// Example: Selecting the Salutation related to the Contact
Select::make('salutation_id') // Use a hidden field for the ID if necessary, or just select the relation directly
->label('Salutation')
->relationship('salutation', 'name') // Tells Filament which relationship to query and which attribute to display
->searchable()
->multiple() // Useful if you ever extend this
// Optional: Customizing how options are displayed (though relationship handles most of it)
// ->getOptionLabelUsing(fn (Salutation $salutation): string => $salutation->name),
Forms\Components\TextInput::make('company_id')
->dehydrated(false),
]);
}
Explanation of the Fix: relationship() Method
The magic here lies in using the relationship() method on a form component (like Select). When you call $form->schema([...]), Filament interprets this instruction. By specifying 'salutation' as the relationship, Filament knows to initiate an Eloquent query based on the current model being edited (Contact), fetching the relevant Salutation via the defined belongsTo relationship.
->relationship('salutation', 'name'): This tells Filament: "Look at thesalutationrelationship on the Contact model, use that to find the related record, and for display purposes, show the value of itsnameattribute."
This approach is superior because it delegates the complex database interaction entirely to Eloquent, which is the core strength of Laravel. You don't need to manually write complex query logic inside getSearchResultsUsing, as Filament handles the necessary scope resolution automatically when using this dedicated method. This aligns perfectly with the principles of clean code and leveraging the power of the framework structure found in projects like those on laravelcompany.com.
Conclusion
Struggling with related data in Filament forms is a rite of passage for many developers, but it ultimately comes down to understanding how to bridge the gap between declarative UI definitions and dynamic Eloquent querying. By moving away from trying to manually construct complex lookups within form components and instead using Filament's dedicated relationship methods like relationship(), you create forms that are not only functional but also maintainable and scalable. Keep focusing on leveraging Laravel’s powerful ORM capabilities, and your Filament development will become significantly smoother!