Laravel - Hasone 5.8

Asked

Viewed 772 times

-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.

  • 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...

  • As reported in the other answer, post your tables let me really see what are the relationships that should be done ...

  • Follows above the area Migration and professionals are same file.

  • It’s what I said is belongsTo with hasMany and has nothing to do with hasOne. So what you want to do this wrong.

1 answer

0

The relationship by Migration added in the last edition credits to say that the relationship is 1 for many where areas and users has several profissionais and profissionais has a area and a usuario

In this relationship that already has example here on the site, is this way:

class Profissionais extends Model
{
    protected $fillable = [
        'area_id',
        'user_id',
        'formacao',
        'rg',
        'observacao'
    ];

    protected $hidden = [];

    public function area() 
    {
        return $this->belongsTo(\App\Areas::class,'area_id','id');
    }
    public function user() 
    {
        return $this->belongsTo(\App\User::class,'user_id','id');
    }
}

Remark: that Model Profissionais had the keys in the interface reversed, but, now they are corrected

class Areas extends Model
{
    protected $fillable = ['name'];

    protected $hidden = [];

    public function profissional() 
    {
        return $this->hasMany(\App\Profissionais::class,'area_id','id');
    }
}

That would be the two relations, as I do not have the general context becomes difficult to exemplify the remaining.

Maybe you wanted the relationship of users and professionals to be 1:1, but, as you did in migrations is 1 to N

The operations are further well explained see attached link.

References

Browser other questions tagged

You are not signed in. Login or sign up in order to post.