How to get fields from One to One relationship?
I got my migrations sort of setup similar to this laravel daily guide: https://laraveldaily.com/lesson/booking-api-laravel/profile-fields
For example in patients migration i got this
Now i can create patients but when i go to edit screen only the patient details (description) shows
How do i load all the fields in the edit form? currently its only loading the patient fields.

25 Replies

What does your Form schema look like??
with UserCreateForm looking like
Does the relationship work properly? If you do
Patient::find(ID)->user()->toArray() does it output the correct data?i ran this
and i see it gets user but not the attributes :/
here is the user model
and here is patient model
IsProfile is this
<?php
namespace App;
use App\Models\User;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
trait IsProfile
{
//Delete connected user when profile is deleted
public static function booted()
{
static::deleting(function ($entity) {
// Delete the associated user
$entity->user()->delete();
});
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}
however when i go to phpmyadmin i do the record correctly and the user id column does link to user table


$entity->user->delete();
What do you mean by "I see it gets user but not the attributes?"
Also, I do not know if you can retrieve the user form like you mention with:
I use in my personal project:
Maybe you can try something similar
If you can not figure it out. Publish your project to GitHub so we can help you further.
im doing a work around so far
trying this
just need to assign the values to the $form object and should be sweet
Actually never mind because this borks the create form
Considering just giving up and making edit forms manually because this functionality is out of scope for Filament.
thanks π thats working (i switched to section because i see it does the same thing but both work).
Just one little boo boo, the enum value isnt getting loaded into the select drop down.
also now it only works for edit, i can not create anymore.
form looks like this
public static function form(Form $form): Form
{
return $form
->schema([
Section::make('User Information')
->relationship('user')
->schema(
UserCreateForm::get(),
Forms\Components\Textarea::make('description')
->required()
->columnSpanFull(),
)
->columns(2),
]);
}
and the usercreateform class
<?php
namespace App\Filament\Traits;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
trait UserCreateForm
{
public static function get(): array
{
return [
TextInput::make('first_name')
->required()
->maxLength(255),
TextInput::make('last_name')
->required()
->maxLength(255),
DatePicker::make('date_of_birth'),
TextInput::make('email')
->email()
->required()
->maxLength(255),
TextInput::make('phone')
//->tel()
->required()
->maxLength(255),
// Forms\Components\DateTimePicker::make('email_verified_at'),
Select::make('gender')
->options(['Male', 'Female'])
->required(),
TextInput::make('password')
->password()
->required()
->maxLength(255),
];
}
}
i had to remove 'user' at the start for the fields to show in edit form
Try this π
Select::make()->enum(Gender::class)Unfortunately now there is another problem
Without the user. before the fields i can edit records, however i am unable to create any.
I have added this before to make the relations work when creating new patient
I think the User is created first and then filled. So the fields need to be nullable.
That shouldn't be needed
when i remove it i get this when creating new patient
how can i make sure the user_id is passed when user is created first?
I think your db schema follows a
BelongsTo relationship. Not a HasOneThere should be a
patient_id on the users table. Not the other way.
https://laravel.com/docs/master/eloquent-relationships#one-to-one
Eloquent determines the foreign key of the relationship based on the parent model name. In this case, the Phone model is automatically assumed to have a user_id foreign key. If you wish to override this convention, you may pass a second argument to the hasOne method:
Eloquent: Relationships - Laravel Upcoming - The PHP Framework For ...
Laravel is a PHP web application framework with expressive, elegant syntax. Weβve already laid the foundation β freeing you to create without sweating the small things.
i got it setup differently.
My web app is a patient management system and there are doctors, patients and contact profiles.
They are all on a BelongsTo relationship to the user model.
i did it like this instead of just using roles because they will have different fields
i got it setup differently.Yes. But you set it up wrong. You shared a model with a
HasOne relationship before, though. It's actually a BelongsTo.On user model it has HasOne for each profile doctor() patient() and the profile models all have the user() belongs to function
The HasOne was in the user model, all profile models have belong to functions
Oh, that's right π
All good π this is my first "real" project so if i made a few boo boos i wonβt be surprised π
I mainly do ASP.NET
Okay, so what's probably is the issue then: Filament saves your model
Patient first, then saves the User. So you need to allow user_id to be nullable.Ah aight ill try that
tried this and i can edit the patients but still cant create any π
i tried adding this trait again
However on the dd line i see this
