Dixit Chopra
Dixit Chopra

Reputation: 77

Access new Config file created in custom classes in Laravel5

How can I access new config file(app_category.php) in Custom class??

Here is how I am trying to access config properties here. I want to create some functions related to config file.

<?php namespace App\Classes {
    use Illuminate\Support\Facades\App;

    class Common {

        public $category = \Config::get('app_category.categories');
        public function getAllCategories()
        {
            return $this->category;
        }

        /**
         * @param mixed $category
         */
        public function setCategory($category)
        {
            $this->category = $category;
        }
    }
}
?>

Upvotes: 0

Views: 889

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 152860

You can't have an expression (in this case it's a function call) as the default value of a member variable. Instead you should assign it in your constructor:

class Common {

    public $category;

    public function __construct(){
        $this->category = \Config::get('app_category.categories');
    }

// etc...

I personally prefer the config() helper function over the facade. I just wanted to let you know it existed in case you didn't already.

$this->category = config('app_category.categories');

Upvotes: 2

Related Questions