// dummpy data from config/custom.php
<?php
return [
'data' => [
'1' => [
'id' => 'some_id',
'key' => 'ajdlkjalsdfjasd',
'key_2' => '890809809',
],
'2' => [
'id' => 'some_id33',
'key' => 'ajdlkjalsdfjasd33',
'key_2' => '890809809333',
]
],
// Default key_2
'key_2' => '22222222222222222',
'track_log' => false
];
How to create Dynamically create config/custom.php config file
I am using a package which reads the configuration from config/custom.php config file. Whenever I add a new entry in database, i have to add entry to that config file manually. is there any way, I can create or modify config/custom.php config file dynamically whenever there is new entry added to database. Please help
// dummpy data from config/custom.php
Ok, I'd probably use model events (linked above) then so it can automatically regenerate the file whenever you change the data in the database table.
Make a query to get the values and build up your array how you'd need it.
$myarray = [
'data' => [
'1' => [
'id' => 'some_id',
'key' => 'ajdlkjalsdfjasd',
'key_2' => '890809809',
],
'2' => [
'id' => 'some_id33',
'key' => 'ajdlkjalsdfjasd33',
'key_2' => '890809809333',
]
],
'key_2' => '22222222222222222',
'track_log' => false
];
// create the array as a php text string
$text = "<?php\n\nreturn " . var_export($myarray, true) . ";";
Then save $text as /config/custom.php
If you're caching the config file, you'd have to run an artisan command after saving the file.
Artisan::call('config:cache');
https://laravel.com/docs/5.6/artisan#programmatically-executing-commands
Please or to participate in this conversation.