F
Filament16mo ago
Malja

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();
});
}
// ,,, 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();
});
}
// ... 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),
])
]);
}
// ... 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.
9 Replies
Malja
MaljaOP16mo ago
Without 'relationship' in the Address section, I'm able to read all address-related values in the handleRecordCreation method. In the worst-case scenario, I can save the relation manually. PS: The name of the relation matches the name of the method in the Customer model:
public function address(): HasOne {
return $this->hasOne(Address::class);
}
public function address(): HasOne {
return $this->hasOne(Address::class);
}
cheesegrits
cheesegrits16mo ago
I think you have your relationship wrong on the Customer model. If the foreign key is on the customers table as address_id, the Customer BelongsTo Address, and Address HasOne Customer. So try changing the relationship on the Customer model to ...
public function address(): BelongsTo {
return $this->belongsTo(Address::class);
}
public function address(): BelongsTo {
return $this->belongsTo(Address::class);
}
I'm actually kinda surprised that didn't error out, as with that relation as a HasOne, it should have tried to save the address record with a customer_id, which doesn't exist.
Malja
MaljaOP16mo ago
Of course, you are right... However even with that change, I am getting following error:
Not null violation: 7 ERROR: null value in column "address_id" of relation "customers" violates not-null constraint
Not null violation: 7 ERROR: null value in column "address_id" of relation "customers" violates not-null constraint
Does relationship work with uuids?
cheesegrits
cheesegrits16mo ago
I'm fairly certain, yes. But I don't use uuids, so don't have firsthand experience. I assume you defined the PK as uuid primary in the migration.
Malja
MaljaOP16mo ago
id of both Customer and Address are created the same way:
$table->uuid('id')->primary();
$table->uuid('id')->primary();
Ashk
Ashk16mo ago
Is it ok for you to use a select + createOptionForm ? https://filamentphp.com/docs/3.x/forms/fields/select#creating-a-new-option-in-a-modal Else, can you try to add a withDefault on your relation ? Else in your CreatePage you can create relation before record created Other thing, do you have $fillable / $guarded attribute in your both models ?
Malja
MaljaOP16mo ago
@Ashk thank you for suggestions. Both models have
protected $guarded = [];
protected $guarded = [];
I know there are multiple ways to create the relation, but I'd prefer something 'built-in' . That's why I am asking.
Ashk
Ashk16mo ago
So try a select or a withDefault on your relation address
Malja
MaljaOP16mo ago
Select with createOptionForm works fine. But again, that's not ideal. For admin users, who know what are they doing, this may be fine. But I cannot expect the end-user to know, that thay have to click on the plus icon and create a new record in modal window. Thank you nevertheless
Want results from more Discord servers?
Add your server