© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•13mo ago•
1 reply
Edward van Tonder

Importing (and creating) data for more than one table

I'm trying to import data, but in the end I want to insert into more than one table.

Problem details:
- I have a
users
users
table.
- I have a
teachers
teachers
table.
- The import should add users to the
users
users
table but it should also create teachers (for new users) for each user

Unless I understand the docs incorrectly, the
->relationship()
->relationship()
helper won't work because I don't have existing teachers to match to users.

My idea was to create teachers (and link them to a user) within the
->afterCreate()
->afterCreate()
or
->afterSave()
->afterSave()
lifecycle hooks but this does not seem to work.
In fact the hooks doesn't seem to fire at all.

Currently I have:
public static function getColumns(): array
    {
        return [
            ImportColumn::make('first_name')
                ->label('First Name')
                ->requiredMapping()
                ->rules(['required', 'string'])
                ->examples(['John', 'Jane'])
                ->exampleHeader('First Name'),

            ImportColumn::make('last_name')
                ->label('Last Name')
                ->requiredMapping()
                ->rules(['required', 'string'])
                ->examples(['Doe', 'Mathews'])
                ->exampleHeader('Last Name'),

            ImportColumn::make('email')
                ->label('Email Address')
                ->requiredMapping()
                ->rules(['required', 'email'])
                ->examples(['john@gmail.com', 'mathews@yahoo.com'])
                ->exampleHeader('Email Address'),
        ];
    }
public static function getColumns(): array
    {
        return [
            ImportColumn::make('first_name')
                ->label('First Name')
                ->requiredMapping()
                ->rules(['required', 'string'])
                ->examples(['John', 'Jane'])
                ->exampleHeader('First Name'),

            ImportColumn::make('last_name')
                ->label('Last Name')
                ->requiredMapping()
                ->rules(['required', 'string'])
                ->examples(['Doe', 'Mathews'])
                ->exampleHeader('Last Name'),

            ImportColumn::make('email')
                ->label('Email Address')
                ->requiredMapping()
                ->rules(['required', 'email'])
                ->examples(['john@gmail.com', 'mathews@yahoo.com'])
                ->exampleHeader('Email Address'),
        ];
    }


public function resolveRecord(): ?User
    {
        return User::firstOrNew(['email' => $this->data['email']]);
    }
public function resolveRecord(): ?User
    {
        return User::firstOrNew(['email' => $this->data['email']]);
    }


  protected function afterSave(): void
    {
        if (!$this->record->teacher) {
            Teacher::create(['user_id' => $this->record->id]);
        }
    }
  protected function afterSave(): void
    {
        if (!$this->record->teacher) {
            Teacher::create(['user_id' => $this->record->id]);
        }
    }


Is there a better approach here? I'm I missing something?
Any help would be appreciated.
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Table layout grid more than one column in mobile
FilamentFFilament / ❓┊help
2y ago
More than one invoice simultaneously.
FilamentFFilament / ❓┊help
3y ago
Not recognized more than one rule
FilamentFFilament / ❓┊help
3y ago
how to create a table composed of more than one model
FilamentFFilament / ❓┊help
11mo ago