Use `relationship()` when creating a new record

Hi all,

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


And 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();
    });
}


When I use 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),
            ])
    ]);
}


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

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

Thank you in advance.
Was this page helpful?