© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•3y ago•
2 replies
jlarm

Add Current User to Pivot on save.

I have a Dealership model and the standard User model setup as a belongsToMany with a pivot table. Very basic, but what I am having issues with is when a user creates a new dealership I want to automatically add the user to the pivot table. Prior to having it a belongsToMany it was a hasMany dealerships so I used a boot method on the Dealership model to add the current user_id to the newly created dealership on save, which worked just fine using:

protected static function booted(): void
{
    parent::boot();

    static::creating(function ($dealership) {
        $dealership->user_id = auth()->user()->id;
    });
});
protected static function booted(): void
{
    parent::boot();

    static::creating(function ($dealership) {
        $dealership->user_id = auth()->user()->id;
    });
});


This is what I currently have for adding user/dealership to the pivot table:

protected static function booted(): void
{
    parent::boot();

    static::created(function ($dealership) {
        $dealership->users()->syncWithoutDetaching(\Auth::id());
    });
});
protected static function booted(): void
{
    parent::boot();

    static::created(function ($dealership) {
        $dealership->users()->syncWithoutDetaching(\Auth::id());
    });
});


I noticed that if I change from "created" to "saved", then create a new dealership then click the "save changes" button after the dealership has been created it will add the relationship, I just cannot get it to add the relationship on creation.

Any help would be appreciated!

Thanks
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

pivot table user/user
FilamentFFilament / ❓┊help
3y ago
Save relation manager order to pivot table
FilamentFFilament / ❓┊help
3y ago
Tenant user pivot values
FilamentFFilament / ❓┊help
2y ago
Custom column to display a pivot of the current user and tenancy relationship
FilamentFFilament / ❓┊help
3y ago