Factory manual incrementing

Posted 6 months ago by spacedog4

I need to set the primary key for my table manually, so I created a function to get the last record and return it +1 (cause my table has "multiple primary keys")

But the problem is that I can't use the multiple method from the factory cause it returns all the same key

<?php

use Faker\Generator as Faker;

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

    $faker->locale('pt_BR');

    if (!$empresa = \App\Empresa::inRandomOrder()->first()) {
        $empresa = factory('App\Empresa')->create(array_only($overrides, \App\Empresa::getPrimaryKeyGroup()));
    }

    return [
        'codigoempresa' => $empresa->codigoempresa,
        'codigoce'      => \App\Controleexportacao::generateKey(), // Here I get the last record key + 1
        'nometabela'    => $faker->word,
        'data'          => $faker->date(),
        'hora'          => $faker->time(),
        'tipo'          => $faker->randomElement(\App\Controleexportacao::tipos()->all()),
        'exportado'     => array_random(['S', 'N']),
    ];
});

I've found this solution https://stackoverflow.com/questions/44983693/laravel-factory-manual-increment-of-column but in my case this doesn't work, cause I can't do something like this

static $codigoce = \App\Controleexportacao::generateKey();

Please sign in or create an account to participate in this conversation.