kazehaya's avatar

Class 'App\Http\Controllers\Config' not found

Im trying to rewrite the database settings for tenants depending on user login.

In my controller i want to use this:

    public function __construct()
    {
        Config::set('database.connections.tenant.username', Auth::user()->projects[0]->username);
        Config::set('database.connections.tenant.password', Auth::user()->projects[0]->password);
        Config::set('database.connections.tenant.database', Auth::user()->projects[0]->database);     
    }

But then im getting this error: Class 'App\Http\Controllers\Config' not found

How can i make this work? :)

0 likes
4 replies
MegaWubs's avatar
Level 1

You need to use Config from the global scope. You're in the namespace App\Http\Controllers and it tries to find Config in that namespace. You can fix it by adding a slash in front of Config::set():

 \Config::set('database.connections.tenant.username', Auth::user()->projects[0]->username);
Harish's avatar

I don't know if this Code or approach is correct to implement but I found this other way round to solve this issue :

Following steps are demonstrated using the example of including constant and a constant.php file in your project -

Step 1 : Add a namespace in your constant.php file (or any file respective of your project) like <?php namespace Forfun; class className { // Your code } ?>

Step 2 : In your respective controllers and eloquent files just add the following code at the top: <?php use ForFun\className;

I hope it helps you guys to write more simple and cleaner code :) revert your suggestions if you find this helpful!

Chill's avatar

There is another cause for this problem. I'll post it here just in case it saves someone down the road...

I had a Laravel app working perfectly fine until at some point it started throwing this exact same error. As it turns out, the ONLY place that showed me the cause was running "composer update". The error was pretty clear:

Dotenv values containing spaces must be surrounded by quotes.

I had added something to my .env file that had spaces in it and I didn't surround it with quotes. So: #.env VALUE=Some spaces

#change it to VALUE="Some spaces "

And all will work again.

Please or to participate in this conversation.