0

I am using Laravel 5.5 and I have a config file in dir app\Json\Schemas\TestSchema.php which contains an array with configurations like this:

return [
  'value' => 'string'
];

What is the best way to include file dynamically?

In my model

namespace App;

use App\Json\Traits\JsonModel;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{ 
    public function getJsonSchema($schema)
    {
        return  'App\\Json\\Schemas\\'.$schema; 
    }
}
coder fire
  • 993
  • 2
  • 11
  • 24

1 Answers1

2

The best way would be to move your config file to the config folder. Lets assume you call it schema.php.

You can load the config file in your code using (where schema is the name of the config file without the .php):

config('schema');

And if you want the value, you can use dot notation (where schema is the name of the config file, and value is a key in the config array):

$value = config('schema.value');

Therefore the getJsonSchema function can be updated to (where $schema is the name of the config file):

public function getJsonSchema($schema)
{
    return  'App\\Json\\Schemas\\'.config($schema.'.value'); 
}
Niraj Shah
  • 15,087
  • 3
  • 41
  • 60