Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mstdmstd's avatar

How in function form of filament get current model ?

In adminarea based on filament 2.17 app with savannabits/filament-flatpickr 1.0 I want to swicth color of flatpickr depending on it it shows expired date , like :

public static function form(Form $form): Form
{
    return $form
        ->schema([
             Flatpickr::make('read_at')->hintColor(  $itemModel->expire->isPast() ? , 'danger',  'primary' ),
     ...

But I did not find how can I get $itemModel in form method (in edit mode ) ? Looking at examples at https://filamentphp.com/docs/2.x/forms/fields I see that Model $record is used in some closure methods, but how can I use in my case?

Thank you!

0 likes
8 replies
LaryAI's avatar
Level 58

To access the current model in the form method of a Filament form, you can use the getCurrentRecord method of the form instance. This method returns the current model instance being edited in the form.

Here's an example of how you can use it to set the color of a Flatpickr field based on whether the expiration date has passed:

use App\Models\Item;
use Filament\Forms\Form;
use Filament\Forms\Fields\Flatpickr;

public static function form(Form $form): Form
{
    $itemModel = $form->getCurrentRecord();

    return $form
        ->schema([
            Flatpickr::make('read_at')
                ->hintColor(
                    $itemModel->expire->isPast() ? 'danger' : 'primary',
                    'primary'
                ),
            // ...
        ]);
}

In this example, we're using the getCurrentRecord method to get the current Item model instance being edited in the form. We then use this instance to check whether the expiration date has passed, and set the color of the Flatpickr field accordingly.

Note that you'll need to import the Item model at the top of your file if you haven't already done so.

mstdmstd's avatar

With code:

    public static function form(Form $form): Form
    {
        $bannerModel = $form->getModel();

I got error :

Call to undefined method Filament\Resources\Form::getModel()

With method getCurrentRecord I dot error that is not found anyway...

At docs https://filamentphp.com/docs/2.x/admin/resources/getting-started I did not find any of these methods...

On

dd($form);

I got output :

Filament\Resources\Form {#5404 ▼ // app/Filament/Resources/BannerResource.php:70
  #columns: 2
  #isDisabled: false
  #isWizard: false
  #modifyBaseComponentUsing: null
  #schema: []
}
mstdmstd's avatar

Looking at valid answer of the branch Laravel Filament Table Actions: url() not working

I try to make as next to recieve current record in “edit” mode :

public static function form(Form $form): Form
{
    $localeInputs = with(new BannerResource)->getLocaleInputs();
    \Log::info(varDump($localeInputs, ' -1 FOUND $localeInputs::'));
    ...
    
    
protected function getLocaleInputs()
{
    return function (Banner $record) {
        return $record->record;
    };

}

But in log file I see :

 (Object of Closure) : -1 FOUND $localeInputs:: : Array
(
    [0] => Closure Object
        (
            [this] => App\Filament\Resources\BannerResource Object
                (
                )

            [parameter] => Array
                (
                    [$record] => <required>
                )
        )
)

But the valid Banner $record as I expected...

mstdmstd's avatar
mstdmstd
OP
Best Answer
Level 8

Decision is using callback method :

                                Flatpickr::make('expires_at')
                                    ->required()
                                    ->dateFormat('Y-m-d')
                                    ->hint(function (Banner|null $record) {
                                        if (empty($record)) {
                                            return null;
                                        }

                                        return $record->expires_at->isPast() ? 'This banner is expired' : '';
                                    })
                                    ->hintIcon(function (Banner|null $record) {
                                        if (empty($record)) {
                                            return null;
                                        }

                                        return $record->expires_at->isPast() ? 'heroicon-s-clock' : '';
                                    })
1 like
karamqubsi's avatar

For filament v2 this worked for me

        $record = static::resolveRecordRouteBinding(request('record'));
jeremykes's avatar

For a quick way to check a bunch of stuff with the form, you can quickly dd the $form variable. For example I wanted to have a simple way of showing a different label during Create and Edit of a User form;

	TextInput::make('password')
         ->label($form->getOperation() == 'create' ? 'Password' : 'Password (Generate New)')

If you dd($form), you will see the attributes that you can call (dirty way of doing a quick check):

	->label(dd($form))

Please or to participate in this conversation.