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.