Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Qnk's avatar
Level 3

faker generates the same numbers on pivot tables

Hi, when I use the factory to generate dummy data for a pivot table, all rows are filled with the same value, I mean: category_id | product_id 1 1 2 2 3 3 and so on

The only one not smart solution was to write something like this:

    public function run()
    {
        factory(App\Product::class,50)
            ->create()
                    ->each(function($u) {
                    $u->categories()->attach(factory('App\Category')->create()
                         ->each(function($u) {
                        $u->categories()
                          ->save(factory('App\Category'));
                    });
            });
    }

Anyway, I still don't know why numbers aren't random generated. I can't use randomDigit from faker in order to don't generate a number which is not a valid id.

Thanks

0 likes
0 replies

Please or to participate in this conversation.