ahmetalan's avatar

How can i use DB Facade in my custom class ?

Hello All,

First i have a helpers folder and my custom class, i want use DB facade in class but i cant, when i try run the code, that gives me Fatal error: Class 'DB' not found in

What's wrong in this code ?

<?php
 namespace AnkaTech\Helpers;
use DB;


 class Helper{
     public static function settings($columnName){
         $setting = DB::table("settings")->where("id",1)->get();
         return $setting->{$columnName};
     }
 }
0 likes
18 replies
bobbybouwmann's avatar

You need to import the facade class instead

use Illuminate\Support\Facades\DB;
ahmetalan's avatar

@bobbybouwmann when i do this that give me this error ;

Fatal error: Uncaught exception 'ReflectionException' with message 'Class log does not exist' ReflectionException: Class log does not exist in

ahmetalan's avatar

No i dont use logClass, only change use DB to use Illuminate\Support\Facades\DB; and that give me this error.

thomaskim's avatar

I'm not sure what's causing your log error, but you should use first and not get. Based on how you're trying to return one field, I'm assuming you just want to fetch 1 entry.

ahmetalan's avatar

@bobbybouwmann when i add use Illuminate\Support\Facades\DB; its not give error on helpers/generate .php its gives a Fatal error: Uncaught exception 'ReflectionException' with message 'Class log does not exist' in E:\dev\wamp\www\ankatech\vendor\laravel\framework\src\Illuminate\Container\Container.php on line 737 ( ! ) ReflectionException: Class log does not exist in E:\dev\wamp\www\ankatech\vendor\laravel\framework\src\Illuminate\Container\Container.php on line 737

jekinney's avatar

What level or where is your helper class in association with the app folder. You may need to auto load the folder in your composer file. If loading correctly the facade should be avalible with your original code.

ahmetalan's avatar

@jekinney my composer.json file looks like

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=5.5.9",
        "laravel/framework": "5.1.*",
        "teepluss/theme": "dev-master",
        "barryvdh/laravel-debugbar": "^2.0"
    },
    "require-dev": {
        "fzaninotto/faker": "~1.4",
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "~4.0",
        "phpspec/phpspec": "~2.1"
    },
    "autoload": {
        "classmap": [
            "database",
            "app/Kodanka/Helpers"
        ],

        "psr-4": {
            "AnkaTech\\": "app/"
        }
    },
    "autoload-dev": {
        "classmap": [
            "tests/TestCase.php"
        ]
    },

    "scripts": {
        "post-install-cmd": [
            "php artisan clear-compiled",
            "php artisan optimize"
        ],
        "pre-update-cmd": [
            "php artisan clear-compiled"
        ],
        "post-update-cmd": [
            "php artisan optimize"
        ],
        "post-root-package-install": [
            "php -r \"copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ]
    },
    "config": {
        "preferred-install": "dist"
    }
}
bestmomo's avatar

What happen with that ?

$setting = \DB::table("settings")->where("id",1)->get();
ahmetalan's avatar

@bestmomo when i do that, thats again give me Class DB Not Found error.

Last night i try create my custom facade and thats working fine without use another facade, for example when i try Config facade thats not working again, same errors, so i dont understand why i cant use laravel facades in my custom facade ?

bestmomo's avatar

@ahmetalan

You can use directly the container :

$setting = app('db')->table('settings')->whereId(1)->first();
ahmetalan's avatar

@bestmomo i try this but again give a error ;

Fatal error: Uncaught exception 'ReflectionException' with message 'Class log does not exist'

i dont understand this why that giving "Class log does not exists" error ?

bestmomo's avatar

@ahmetalan

Maybe the class log doesn't exists ^^

What do you get with this ?

dd(app('log'));

There is something weird with your Laravel, try delete all vendor and reinstall it.

ahmetalan's avatar

@bestmomo still same error, i trying in fresh laravel, version 5.1.24

thats really weird :)

craigchilds94's avatar
Level 4

It looks as though one of the dependencies hasn't been loaded correctly. Like @bestmomo has suggested, remove your /vendor folder and try re-installing your composer dependencies?

If that doesn't seem to work, I'd suggest looking at the underlying dependencies to see if there's a version mis-match or your version of php doesn't support x, or x isn't enabled. It could be something really simple but not obvious.

ahmetalan's avatar

İ solved problem with @craigchilds94 answer, the problem is about my php version. Now i using homestead, its working fine. thanks for all..

1 like

Please or to participate in this conversation.