Laravel Filament. with options and default value

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Mastering Dynamic Relationships in Filament: Handling Options and Default Values As developers building complex applications with Laravel and Filament, we often run into scenarios where standard Eloquent relationships fall short. One particularly thorny issue arises when dealing with nested data, circular dependencies, and, most importantly, dynamic access control—like ensuring a user can only select records they are authorized to see. I recently encountered a situation within a Filament `DomainResource` where I needed to implement a relationship field that dynamically changes its available options based on the currently authenticated user's roles. Trying to use the built-in `relationship()` method proved cumbersome, especially when dealing with circular relationships or conditional filtering. This post dives into the architectural solution for managing dynamic select fields in Filament forms, focusing on how to correctly handle options and default values while respecting Laravel's Eloquent capabilities. ## The Challenge: Circularity and Access Control The core problem revolves around displaying a list of related models (e.g., users) within a form field. When you use `relationship('user', 'name')`, Filament assumes a direct, unrestricted path. However, if the relationship itself is complex or if certain users should be excluded from the selection pool based on their permissions, this simple approach breaks down. The initial attempts often led to subtle bugs: either the list appeared empty, or worse, the logged-in user still had the ability to input data they shouldn't have access to, bypassing intended security checks. This highlights a common pitfall: mixing presentation logic (Filament) with data access logic (Eloquent permissions). ## Solution 1: Dynamic Options via Closures in `options()` The most effective way to solve dynamic filtering is to bypass the automatic relationship handling and manually construct the options using a closure within the form field definition, specifically utilizing the `options()` method. This gives us full control over the data presented to the user. When dealing with conditional logic based on roles (like checking if the user is a 'super-admin'), we can leverage Eloquent queries directly inside the closure. Consider the transformation from a simple relationship call to a dynamic option builder: ```php Select::make('user_id') // Instead of relying on a direct relationship, we build the options manually. options(function () { $currentUser = auth()->user(); if ($currentUser->hasRole('super-admin')) {