Return datatype from .env in Laravel
Asked Answered
O

1

6

I want to have dynamic seeders based on my environment. (eg in testing I want so only seed 100 rows, whereas local it would be 10'000).

I've created seeder.php in the config, which is calling values from the .env file.

When I use the config in my seeder, it's returning a string on a value in the .env that should be an integer. EG:

.env.local:

SEED_USER_COUNT=10000

config\seeder.php:

return [
    'user_count' => env('SEED_USER_COUNT', 10),
];

UserSeeder

factory(User::class, config('user_count'))->create();

The above fails to work and it appears that config('user_count') returns a string "10000" rather than the intger 10000

Oskar answered 17/1, 2020 at 15:32 Comment(1)
can you just cast to int to solve that, like factory(User::class, (int) config('user_count'))->create();Prytaneum
H
14

Cast your variable to int in the configuration file

config/seeder.php

return [
    'user_count' => (int)env('SEED_USER_COUNT', 10),
];

Call the configuration correctly with the prefix of the file

UserSeeder

factory(User::class, config('seeder.user_count'))->create();
Hesperides answered 17/1, 2020 at 16:11 Comment(2)
That works! Thanks (fwiw I was calling the config value correctly in the project, just mis-typed here).Oskar
it's work , thanksTasso

© 2022 - 2024 — McMap. All rights reserved.