Cannot store other data in Form Select relationship
Good day, in some modules it's working... but in this module is not working,, I want to add gender of the user but the userDetails is separate from users table
public function detail(): HasOne
{
return $this->hasOne(UserDetail::class, 'id');
}
UserDetail
<?php
namespace App\Models;
use App\Cache\UserDetail\UserDetailById;
use App\Enums\UserDetail\Gender;
use App\Models\Contracts\Cacheable;
use App\Observers\UserDetailObserver;
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
#[ObservedBy([UserDetailObserver::class])]
class UserDetail extends Model implements Cacheable
{
use HasFactory;
protected $fillable = ['birthdate','gender'];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'birthdate' => 'date',
'gender' => Gender::class,
];
}
/*
|--------------------------------------------------------------------------
| Custom Methods
|--------------------------------------------------------------------------
*/
protected static function booted()
{
static::saving(function ($userDetail) {
\Log::info('Gender being saved:', ['gender' => $userDetail->gender]);
});
}
/**
* Flushes the model cache.
*/
public function clearCache(): void
{
(new UserDetailById($this->id))->invalidate();
}
}
I have only done this with Relationship Managers in Filament 3.x
Schema wise you may want to consider putting the user details in the user model. In my opinion, it makes more sense since most of the user details in the information is immutable and also will never be a OneToMany relation.