© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•3y ago•
13 replies
Malja

Use `relationship()` when creating a new record

Hi all,

I have two tables,
consumers
consumers
and
addresses
addresses
. The consumer has a
address_id
address_id
column:
// ,,, migration for consumer table
public function up(): void
{
    Schema::create('customers', function (Blueprint $table) {
        // ... other columns
        $table->foreignUuid('address_id')->constrained();
    });
}
// ,,, migration for consumer table
public function up(): void
{
    Schema::create('customers', function (Blueprint $table) {
        // ... other columns
        $table->foreignUuid('address_id')->constrained();
    });
}


And
addresses
addresses
table looks like this:

// ... migration for addresses table
public function up(): void
{
    Schema::create('addresses', function (Blueprint $table) {
        $table->uuid('id')->primary();

        $table->string('first_line');
        $table->string('second_line')->nullable();

        $table->string('city')->nullable();
        $table->string('state')->nullable();
        $table->string('country')->nullable();

        $table->timestamps();
        $table->softDeletes();
    });
}
// ... migration for addresses table
public function up(): void
{
    Schema::create('addresses', function (Blueprint $table) {
        $table->uuid('id')->primary();

        $table->string('first_line');
        $table->string('second_line')->nullable();

        $table->string('city')->nullable();
        $table->string('state')->nullable();
        $table->string('country')->nullable();

        $table->timestamps();
        $table->softDeletes();
    });
}


When I use
relationship
relationship
method to create Address for Consumer:

// ... inside CustomerResource.php
public static function form(Form $form): Form
{
    return $form->schema([
        Forms\Components\Section::make('General')->schema([
            // ... all fields for consumer
        ]),

        Forms\Components\Section::make('Address')
            ->relationship('address')
            ->schema([
              Forms\Components\TextInput::make('first_line')
                ->required()
                ->maxLength(255),

            Forms\Components\TextInput::make('second_line')
                ->required()
                ->maxLength(255),

            Forms\Components\TextInput::make('country')
                ->required()
                ->maxLength(255),
            ])
    ]);
}
// ... inside CustomerResource.php
public static function form(Form $form): Form
{
    return $form->schema([
        Forms\Components\Section::make('General')->schema([
            // ... all fields for consumer
        ]),

        Forms\Components\Section::make('Address')
            ->relationship('address')
            ->schema([
              Forms\Components\TextInput::make('first_line')
                ->required()
                ->maxLength(255),

            Forms\Components\TextInput::make('second_line')
                ->required()
                ->maxLength(255),

            Forms\Components\TextInput::make('country')
                ->required()
                ->maxLength(255),
            ])
    ]);
}


then the Address is not created. In addition, when I use the
handleRecordCreation
handleRecordCreation
method, I don't see any address-related data in there. Only data for Customer.

Does the
relationship()
relationship()
method work for creating new records? Where could be the problem?

Thank you in advance.
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

Missing Required Parameter when Creating a New Record
FilamentFFilament / ❓┊help
3y ago
Default Key-Value when creating a new record
FilamentFFilament / ❓┊help
3y ago