3

I am creating a library that uses \Illuminate\Support\Facades\DB to reference the database. I'm getting the following exception on the call to DB::table()->where()->get(): "ReflectionException: Class db does not exist." What is the problem and what do I need to do to overcome it?

I know that there is a process using \Illuminate\Database\Capsule\Manager to establish a connection to the database. But I haven't found any example on how to make this work with \Illuminate\Support\Facades\DB.

Code sample:

use \Illuminate\Support\Facades\DB;

abstract class Data
{
    public static function tables($prefix, $mode=null)
    {
        $outbound = array();
        $tables = DB::table('information_schema.tables')->where('TABLE_NAME', 'LIKE', $prefix.'_%')->get();
        foreach ($tables as $table) {
            ...
        }
        return $outbound;
    }
}

Stack trace:

~/vendor/laravel/framework/src/Illuminate/Container/Container.php:749
~/vendor/laravel/framework/src/Illuminate/Container/Container.php:644
~/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:709
~/vendor/laravel/framework/src/Illuminate/Container/Container.php:1203
~/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:175
~/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:144
~/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:231
~/Data.php:XXX
Jay Bienvenu
  • 3,069
  • 5
  • 33
  • 44

4 Answers4

0

Since it's a facade, add this to the top of the class to make it work:

use DB;

Or use full namespace:

$tables = \DB::table...
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
0

this is not correct way but you can write this way. give a '/' before DB

$tables = \DB::table('information_schema.tables')->where('TABLE_NAME', 'LIKE', $prefix.'_%')->get()

Sibasankar Bhoi
  • 589
  • 4
  • 14
0

TL;DR:

Temporarily commenting out lines with DB:: and adding https://stackoverflow.com/a/39354471/729077 should help you figure out the real reason.

Longer explanation:

One of the reasons could be similar to those listed in this thread: Uncaught ReflectionException: Class log does not exist Laravel 5.2, the only difference being db instead of log class that's missing.

It seems to depend on which class occurrence happens first in code - in yours and mine it was DB, while usually it is Log facade that triggers the error.

For me, the reason was missing 3rd party package, with its service provider added already to list in config/app.php. In result, every artisan command spat Call to undefined method Illuminate\Support\Facades\DB::connection() error. After adding explicit use Illuminate\Support\Facades\DB to top of offending file, the error changed to yours (class db does not exist).

I've added code from https://stackoverflow.com/a/39354471/729077, then commented out the line with DB::connection() and artisan has shown the real culprit. After composer requireing missing package, laravel started working again.

Community
  • 1
  • 1
bjauy
  • 929
  • 16
  • 22
0

If someone else is getting this when running phpunit tests, I was able to fix it by extending Tests\TestCase instead of PHPUnit\Framework\TestCase

e-e
  • 1,071
  • 1
  • 11
  • 20