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

mikromike's avatar

Filamentphp3, how get model id after form has been saved?

Hello

So Filament 3.2, and Laravel 10.10., code from class BannerResource

   return $form
        ->schema([
            Forms\Components\Group::make()
            ->schema([ Forms\Components\Select::make('sites')
                    ->multiple()
                    ->preload()
                    ->relationship('sites', 'name')                       
                    ->afterStateUpdated(function (array $state){
                        // dump($state);   // This is array of multialbe Site ids
                       }),
     Forms\Components\Section::make()
                ->schema([                    
                    FileUpload::make('image_name')                                               
                    ->directory('images')
                    ->visibility('public')
                    ->preserveFilenames()         
                     ->image()
                   ->imageEditor()
                   ->afterStateUpdated(function (Livewire $livewire){
                     //    dump($livewire);   // Get information beforemodel has been saved.
                     // (function (Component $component)
                   }),

Q) How to get saved model id?

Added new method to class CreateBanner extends CreateRecord called end of form

      public static function form(Form $form): Form
     {   

     return $form
         ->schema([
        /// all form code here
      ])->afterSave();     // last line of form

    public function afterSave($record): void
   {
     $savedModelId = $record->id;

     dd($savedModelId);
 }

Thanks Mika.

0 likes
1 reply
LaryAI's avatar
Level 58

Hello Mika,

In Filament, you can use the afterSave callback to perform actions after the form has been saved. You've already added the afterSave method to your CreateBanner class, which is the right approach. However, you need to ensure that you're passing the callback correctly to the afterSave method in the form definition.

Here's how you can modify your CreateBanner class to get the saved model's ID:

use Filament\Resources\Form;
use Filament\Resources\Pages\CreateRecord;

class CreateBanner extends CreateRecord
{
    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                // all form code here
            ])
            ->afterSave(function ($record) {
                $savedModelId = $record->id;
                dd($savedModelId);
            });
    }
}

In this code, the afterSave method is passed as a closure to the afterSave method of the form. This closure receives the $record parameter, which is the instance of the model that has just been saved. You can then access the id property of the $record to get the saved model's ID.

Make sure that you're using the correct namespace for the Form and CreateRecord classes if they are located in a different namespace in your application.

I hope this helps you get the saved model's ID in Filament. If you have any further questions, feel free to ask!

Please or to participate in this conversation.