-1
I have no problems with registering related data in Hasone I have doubt about registering 2 hasOne data...
I know that to register two hasOne data is as follows example:
$users = User::create([
'name' => $request->nome,
'email' => $request->email,
'cpf' => $request->cpf,
'nascimento' => $request->nascimento,
'password' => bcrypt($request->password),
])->profissionais()->create([
'formacao' => $request->formacao,
'rg' => $request->rg,
'observacao' => $request->observacao
]);
As you can see is the same code without Area_id, my problem is the area_id need to relate it to professionals too and it is a hasOne data.
I have the following code:
$users = User::create([
'name' => $request->nome,
'email' => $request->email,
'cpf' => $request->cpf,
'nascimento' => $request->nascimento,
'password' => bcrypt($request->password),
])->profissionais()->create([
'area_id' => $request->area,
'formacao' => $request->formacao,
'rg' => $request->rg,
'observacao' => $request->observacao
]);
$role = Role::where('name', 'Profissionais')->first();
$user = User::where('email', $request->email)->first();
$user->roles()->attach($role->id);
The professional table, has 2 related data of User and Area, to using hasOne relationship but when registering related data, I can not register the two data the professional model is like this:
class Profissionais extends Model
{
protected $fillable = [
'area_id',
'formacao',
'rg',
'observacao'
];
protected $hidden = [];
public function area() {
return $this->hasOne(\App\Areas::class, 'id', 'area_id');
}
public function user() {
return $this->hasOne(\App\User::class, 'id', 'user_id');
}
}
Migration:
Schema::create('areas', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Schema::create('profissionais', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->unsignedInteger('area_id');
$table->string('formacao');
$table->char('rg')->unique();
$table->string('observacao', 200)->nullable();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('area_id')->references('id')->on('areas')->onDelete('cascade');
$table->timestamps();
})
I need to register professional related to these two tables, was a scope suggested by my client, which best solution for this?
Note: hasOne relationship does not accept Attach...
Mine is different it there only makes relationship with 1 given, if you repair well have area_id I did also hasOne then it is 2 hasOne data.
– Lucas Antonio
Just to score notes right the other’s doubt with mine, his doubt I solved days ago my doubt is to relate 2 hasOne data in 1 table...
– Lucas Antonio
As reported in the other answer, post your tables let me really see what are the relationships that should be done ...
– novic
Follows above the area Migration and professionals are same file.
– Lucas Antonio
It’s what I said is belongsTo with hasMany and has nothing to do with hasOne. So what you want to do this wrong.
– novic