0

necesito llenar mis tablas de mi base de datos de las siguiente manera. un ejemplo tengo la tabla plan, auditorias,objetivo_General. se debe llenar 3 planes y para cada plana 4 auditorias y 4 objetivos generales para cada auditoria.

esto es una idea de lo que tengo pero no me sale ayuda porfavor

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]);

        });

los factorys son estos

$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(),
    ];
});

1 respuesta 1

1

Puedes hacer seed de tus relaciones de la siguiente forma (tomando en cuenta que el modelo App\Models\Plan tiene una relación de nombre auditorias:

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

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.