© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•2y ago•
8 replies
_andypeacock

How to save morphToMany relationship during create?

Hi, I'm getting the error "Integrity constraint violation: 1048 Column 'categorizable_id' cannot be null" when laravel tries to run

insert into `categorizables` (`categorizable_id`, `categorizable_type`, `category_id`) values (?, App\Models\Content, 3)
insert into `categorizables` (`categorizable_id`, `categorizable_type`, `category_id`) values (?, App\Models\Content, 3)


The model has the following:

public function categories() : MorphToMany
    {
        return $this->morphToMany(Category::class, 'categorizable');
    }
public function categories() : MorphToMany
    {
        return $this->morphToMany(Category::class, 'categorizable');
    }


And the filament resource form has:

Select::make('categories')
->multiple()
->relationship(titleAttribute: 'name')
->createOptionForm([
    TextInput::make('name')
    ->required(),
    TextInput::make('description')
    ->required(),
    Select::make('parent_id')
    ->options(
        Category::get()
            ->pluck('name', 'id')
    )
])
->preload()
->visible(function (Get $get) {
    return $get('type') === 'post';
})
Select::make('categories')
->multiple()
->relationship(titleAttribute: 'name')
->createOptionForm([
    TextInput::make('name')
    ->required(),
    TextInput::make('description')
    ->required(),
    Select::make('parent_id')
    ->options(
        Category::get()
            ->pluck('name', 'id')
    )
])
->preload()
->visible(function (Get $get) {
    return $get('type') === 'post';
})

Obviously, the relationship can't be saved till after the parent Content model has been saved, but this seems be trying to do that in the reverse order.

Any idea how to get round this? I can't believe I'm the first person to encounter this, so it feels like something I've set up wrong.

Andy
Solution
Hmm. That's really wierd. I've just created a test app with the same models, and it works fine.

OK, so something odd about my setup, so I'll look into it further. Appreciate your help, @toeknee
Jump to solution
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

MorphToMany Select Relationship
FilamentFFilament / ❓┊help
16mo ago
Save nested relationship on create
FilamentFFilament / ❓┊help
3y ago
How to save relationship first
FilamentFFilament / ❓┊help
2y ago
ReplicateAction. How save relationship
FilamentFFilament / ❓┊help
3y ago