Improve bank insertion of related entities?

2

I have the database User , Professional and Address in the database. The foreign key for User is in Professional and the foreign key for Professional is in Address . I need to register a professional, who is a user and has an address.

Model User:

public function profissional()
{
    return $this->hasOne('App\Profissional', 'users_id');
}

Professional Model:

public function endereco()
{
    return $this->hasOne('App\Endereco');
}

public function user()
{
    return $this->belongsTo('App\User', 'users_id');
}

Model Address:

public function profissional()
{
    return $this->belongsTo('App\Profissional');
}

The way I found to insert the professional in the bank along with the other entities was this:

$user = new User($userData);
$profissional = new Profissional($profissionalData);
$endereco = new Endereco($enderecoData);

$user->save();
$user->profissional()->save($profissional);
$profissional->endereco()->save($endereco);

The code is working normally, but I believe there are better ways to do the same thing. I would like some workarounds or improvements that can be made to this code.

    
asked by anonymous 04.10.2016 / 21:43

1 answer

2

This is how you did it can be done to create it too:

$user->create(array())
     ->profissional()->create(array())
     ->endereco()->create(array());

But, your logic is within the standard of Laravel

    
04.10.2016 / 22:01