Malja
Malja
FFilament
Created by Malja on 10/20/2023 in #❓┊help
Searchable select using model's attribute as label
Hi all, I have a model Pet, that belongs to User:
class Pet extends Model {
// ...
public function owner(): BelongsTo {
// This sets the field name in Pet, right? ↓
return $this->belongsTo(User::class, 'owner_id');
}
// ...
}
class Pet extends Model {
// ...
public function owner(): BelongsTo {
// This sets the field name in Pet, right? ↓
return $this->belongsTo(User::class, 'owner_id');
}
// ...
}
class User extends Model {
// ...
public function pets(): HasMany {
// This sets the field name in Pet, right? ↓
return $this->hasMany(Pet::class, 'owner_id');
}
// ...
public function fullName(): Attribute {
return Attribute::make(
get: fn () => "{$this->first_name} {$this->last_name}"
);
}
// ...
}
class User extends Model {
// ...
public function pets(): HasMany {
// This sets the field name in Pet, right? ↓
return $this->hasMany(Pet::class, 'owner_id');
}
// ...
public function fullName(): Attribute {
return Attribute::make(
get: fn () => "{$this->first_name} {$this->last_name}"
);
}
// ...
}
However, I have a problem creating a new Pet for a specific User:
// from PetResource.php
public static function form(Form $form): Form {
return $form->schema([
Forms\Components\Select::make('owner_id')
->label('Owner')
->getSearchResultsUsing(fn ($search): array =>
User::query()
->where('first_name', 'ilike', "%{$search}%")
->orWhere('last_name', 'ilike', "%{$search}%")
->get()
->toArray()
)
->searchable()
->getOptionLabelUsing(fn ($value): string => User::query()->find($value->id)?->fullName)
->required()
]);
}
// from PetResource.php
public static function form(Form $form): Form {
return $form->schema([
Forms\Components\Select::make('owner_id')
->label('Owner')
->getSearchResultsUsing(fn ($search): array =>
User::query()
->where('first_name', 'ilike', "%{$search}%")
->orWhere('last_name', 'ilike', "%{$search}%")
->get()
->toArray()
)
->searchable()
->getOptionLabelUsing(fn ($value): string => User::query()->find($value->id)?->fullName)
->required()
]);
}
The problem is, that no matter what I do in getSearchResultsUsing and getOptionLabelUsing, I won't get User's fullName to be shown. I tried: - Using pluck('last_name', 'id') instead of get. This shows the last_name, but getOptionLabelUsing is not called. - Using relationship('owner', 'last_name'). This does not allow using fullName instead of lastName.
Do you have any suggestions? Thank you.
2 replies
FFilament
Created by Malja on 8/29/2023 in #❓┊help
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.
14 replies