Hello, Laracasts! I have four models: Card,Document_Card, Person and Document_Person
Relations are:
for Card model
<?php
class Card extends Model
{
protected $table ='Cards';
protected $primaryKey='id';
public function Document_Card()
{
return $this->belongsTo('App\Document_Card','Doc_id');
}
public function Person()
{
return $this->hasMany('App\Person','card_id');
}
}
for Document_Card
<?php
class Document_Card extends Model
{
public function Card()
{
return $this->hasMany('App\Card','Doc_id');
}
}
for Person model
class Person extends Model
{
protected $primaryKey='id';
protected $table='Person';
public function Document_Person()
{
return $this->belongsTo('App\Document_Person','doc_id');
}
public function Card() //how to seed this relation ????
{
return $this->belongsTo('App\Card','card_id');
}
}
and Document_Person
<?php
class Document_Person extends Model
{
public function Person()
{
return $this->hasMany('App\Person','doc_id');
}
}
I make PersonFactory
use Faker\Generator as Faker;
$factory->define(App\Person::class, function (Faker $faker) {
return [
.................................
'work'=>$faker->text($maxNbChars = 90) ,
'task'=>$faker->text($maxNbChars = 80) ,
'number'=>$faker->numberBetween($min = 1, $max = 6),
.................................
];
});
Document_PersonFactory
use Faker\Generator as Faker;
$factory->define(App\Document_Person::class, function (Faker $faker) {
return [
'Date'=>$faker->dateTimeBetween($startDate = '-2 months', $endDate = '+2 months', $timezone = null),
'Name'=>$faker->sentence($nbWords = 3, $variableNbWords = true),
];
});
and Document_PerosnSeed
use Illuminate\Database\Seeder;
class Documents_IspsSeed extends Seeder
{
public function run()
{
factory(App\Document_Person::class, 10)->create()->each(function ($doc) {
$doc->ispol()->save(factory(App\Ispol::class)->make());
});
}
} //It makes 10 Document_Person instances and put it into Person table - this relation seeds good
as wel as Factory and Seeder for Card and dDcument_Card models
<?php
//Factory for Card Model
use Faker\Generator as Faker;
$factory->define(App\Card::class, function (Faker $faker) {
return [
'Number' => $faker->numberBetween($min = 7133, $max = 100000),
'Note'=>$faker->text($maxNbChars = 60) ,
...................................................................
];
});
and Factory for Document_Card model
<?php
use Faker\Generator as Faker;
$factory->define(App\Document_Card::class, function (Faker $faker) {
return [
'Description'=>$faker->sentence($nbWords = 3, $variableNbWords = true),
...................................................................
];
});
and Seed at the end)
use Illuminate\Database\Seeder;
class DocumentSeed extends Seeder
{
public function run()
{
factory(App\Document_Card::class, 10)->create()->each(function ($doc) {
$doc->ispol()->save(factory(App\Card::class)->make());
});
}
}//It makes 10 Document_Card records and put id's into Card table - so this relation seeding good as well
But question is:
how to seed
public function Person()
{
return $this->hasMany('App\Person','card_id');
}
relation together with another relations ?? Please help