Hi every one i have user has applicant relationships

i need to register user in applicant resource and direct create applicant.
12 Replies
toeknee
toeknee11mo ago
Please explain in more detail.
Ahmed Ali
Ahmed AliOP11mo ago
i have form for create applicant in applicant resource and in same time there are some inputs for create user that has one applicant relationships (note: the user that i will created is from user table and the applicant from applicant table )
Ahmed Ali
Ahmed AliOP11mo ago
SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value INSERT INTO interviewers (name, updated_at, created_at) VALUES (khaled, 2024-01-04 09:49:39, 2024-01-04 09:49:39) ->schema([ // Forms\Components\Select::make('user_id') // ->relationship('user', 'name') // ->required(), Fieldset::make('user_id') ->relationship('user') ->schema([ TextInput::make('name'), TextInput::make('email'), TextInput::make('password'), ]), Forms\Components\TextInput::make('name') ->required() ->maxLength(255), ]);
toeknee
toeknee11mo ago
user_id is not in fillable on the interviewers relationship.
Ahmed Ali
Ahmed AliOP11mo ago
protected $fillable = [ 'name', 'user_id', ]; // Forms\Components\Select::make('user_id') // ->relationship('user', 'name') // ->required(), this was work and user_id is exit Schema::create('interviewers', function (Blueprint $table) { $table->id(); $table->string('name'); $table->foreignId('userid')->constrained(); $table->timestamps(); $table->softDeletes(); }); now i need to add applicant and its user in one to one relationship. this the previous question any help? and also i need to take the user id for that applicant and assign role to it note: role add by assignRole method by spatie? any one @toeknee ? i meant interviewer not applicant guys help <-->
toeknee
toeknee11mo ago
Please do not tag people and read #✅┊rules You are not showing your current error, and we are also not here to serve. We will help and as we can
Ahmed Ali
Ahmed AliOP11mo ago
i respect thanks just my problem in simple i have user auth table and applicant table, user has one applicant then i creatd applicant resource an i need to create applicant before that create its user this first one the second i have method in user model to assingRole to that user created with applicant thanks is there another way to solve this issues
jlove1672
jlove167211mo ago
Are your users already created before you create an applicant? Or do you create the user and the applicant at the same time?
Ahmed Ali
Ahmed AliOP11mo ago
it the same time
jlove1672
jlove167211mo ago
Ok so what you need to do is simplify your form for creating the applicant All your form needs is:
TextInput::make('name'),
TextInput::make('email'),
TextInput::make('password'),
TextInput::make('name'),
TextInput::make('email'),
TextInput::make('password'),
Then inside your applicant resource you should have a CreateApplicant.php file Inside that file you need to add a function
protected function mutateFormDataBeforeCreate(array $data): array
{
$data['user_id'] = auth()->id();

return $data;
}
protected function mutateFormDataBeforeCreate(array $data): array
{
$data['user_id'] = auth()->id();

return $data;
}
Inside this function ^ you can intercept the data and use it to create a user for the applicant so something like this
protected function mutateFormDataBeforeCreate(array $data): array
{
//create new user manually with the data
$user = User::create(['name' => $data['name']])

$data['user_id'] = user->getKey();

return $data;
}
protected function mutateFormDataBeforeCreate(array $data): array
{
//create new user manually with the data
$user = User::create(['name' => $data['name']])

$data['user_id'] = user->getKey();

return $data;
}
I hope this helps, let me know if you have any issues with it or if it solves your problem
Ahmed Ali
Ahmed AliOP11mo ago
yes i will try it goes well 👍
Want results from more Discord servers?
Add your server