Doubt relationship One to Many

0

You talk, guys? Next, to head with the seed of my BD in laravel, I believe that anyone with more experience with the framework knows how to solve.

I have the model Person with relationship One to Many for other two (Professional and Client). At the time of the migration, it turns out that the registered people end up being professionals and clients at the same time, because they take the same ids, as I determine, for example, 10 registration of id 1 to 5 will be clients and id 6 to 10 Are they professional?

class Pessoa extends Model {

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

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

Professional Model

class Profissional extends Model
{
public function pessoa(){
    return $this->belongsTo('App\Pessoa');
}

Model Client

class Cliente extends Model
{
public function pessoa(){
    return $this->belongsTo('App\Cliente');
}
}

My seed (Here is the error >: p)

factory(App\Pessoa::class, 10)->create()->each(function ($u) { 
$u->cliente()->
save(factory(App\Cliente::class)->make());
$u->profissional()->
save(factory(App\Profissional::class)->make());  
});
    
asked by anonymous 13.08.2018 / 21:27

1 answer

0

You can make the processes separate, since the Person is never customer and professional at the same time. Example:

 factory(App\Pessoa::class, 5)->create()->each(function ($u) { 
    $u->cliente()->save(factory(App\Cliente::class)->make());
 });

 factory(App\Pessoa::class, 5)->create()->each(function ($u) { 
    $u->profissional()->save(factory(App\Profissional::class)->make());  
 });

You could also do the factories within a for, with the count and the rules that you quoted, but I think it will be more annoying to maintain.

    
14.08.2018 / 14:29