how to make a faker with several related tables in laravel?

0

I need to fill my tables in my database in the following way. an example I have the plan table, audits, objective_General. 3 plans must be completed and for each plan 4 audits and 4 general objectives for each audit.

This is an idea of what I have but I do not get help please

factory(\App\Models\Plan::class, 3)->create()->each(function ($planes) {

            factory(\App\Models\Auditoria::class, 4)->create(['codPlanA'=>$planes->codPlanA]);

            factory(\App\Models\ObjetivoGeneral::class, 4)->create(['codPlanF'=>$planes->codPlanF]);

        });
  

the factorys are these

$factory->define(App\Models\Plan::class, function (Faker $faker) {

        return [
            'nombrePlan' => 'PLAN 2017 '.$faker->randomNumber(),
        ];
    });



    $factory->define(App\Models\Auditoria::class, function (Faker $faker) {

        $planes = \App\Models\Plan::pluck('codPlanA');
        $periodoInicio = $faker->date($format = 'Y-m-d');
        $periodoFinPlanF = strtotime('+3 month', strtotime($periodoInicio));
        $periodoFinPlanF = date('Y-m-d', $periodoFinPlanF);


        $fechaIniPlanF = $faker->date($format = 'Y-m-d');
        $fechaFinPlanF = strtotime('+3 month', strtotime($fechaIniPlanF));
        $fechaFinPlanF = date('Y-m-d', $fechaFinPlanF);


        return [
            'nombrePlanF' => 'Auditoria ' . $faker->numberBetween(1, 5),
            'codigoServicioCP' => $faker->randomNumber() . '-4669-2016-001',
            'tipoServicioCP' => 'AUDITORÍA DE CUMPLIMIENTO',
            'organoCI' => 'OCI – Z.R.N°XIII',
            'entidadAuditada' => 'ZONA REGISTRAL N°XIII SEDE TACNA',
            'origen' => 'LA AUDITORÍA DE CUMPLIMIENTO A LA ZONA REGISTRAL N°XIII SEDE TACNA, CORRRESPONDE A UN SERVICIO DE CONTROL POSTERIOR PROGRAMADO EN EL PLAN ANUAL DE CONTROL 2016 DEL ÓRGANO DE CONTROL INSTITUCIONAL, APROBADO MEDIANTE RESOLUCIÓN DE CONTRALORÍA N° 067-2016-CG DEL 15 DE FEBRERO DE 2016, REGISTRADO EN EL SISTEMA SCG, CON EL CÓDIGO N° 2-4669-2016-001. LA COMISIÓN AUDITORA COMUNICÓ EL INICIO DE LA AUDITORÍA CON EL OFICIO N° 021-2016/Z.R.N°XIII-OCI DE 1 DE ABRIL DE 2016.',
            'tipoDemanda' => $faker->sentence($nbWords = 2, $variableNbWords = true),
            'fechaIniPlanF' =>$fechaIniPlanF,
            'fechaFinPlanF' =>$fechaFinPlanF,
           'periodoIniPlanF' => $periodoInicio,
             'periodoFinPlanF' => $periodoFinPlanF,
            'estadoAuditoria' => $faker->randomElement($array = array('PENDIENTE', 'ACTIVO', 'TERMINADO')),
            'codPlanA' => $planes->random(),
        ];
    });


$factory->define(App\Models\ObjetivoGeneral::class, function (Faker $faker) {

    $auditorias = \App\Models\Auditoria::pluck('codPlanF');

    return [
        'nombre' => $faker->sentence($nbWords = 2, $variableNbWords = true),
        'codPlanF' => factory(\App\Models\Auditoria::class)->create(),
    ];
});
    
asked by ingswsm 04.11.2017 в 01:06
source

1 answer

0

You can seed your relationships in the following way (taking into account that the model App\Models\Plan has a name relation auditorias :

factory(\App\Models\Plan::class, 3)->create()->each(function ($planes) {
    $planes->auditorias()->save(factory(\App\Models\Auditoria::class)->make());
});
    
answered by 04.11.2017 в 01:19