4

I am trying to use DB class to run query. I have used it in my controller like this:

use \DB;

And I am using it in my code like this:

$changeCar = DB::select(DB::raw($query));

I have tried including it like this:

use Illuminate\Support\Facades\DB

But still the same. Also, tried it with the backslash in the code and not including it like this:

$changeCar = \DB::select(\DB::raw($query));

My Code:

namespace App\Http\Controllers;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use \DB;


class TicketsController extends Controller
{
    public function myTickets()
    {
        $user = Auth::user();
        $changeCar = DB::select(DB::raw($query));
    }
}

Still the same. What am I doing wrong here? Any help?

P.S: My laravel version is 5.4.33

3 Answers 3

7

DB is a facade in Laravel. So just doing Use DB; should work. Can you check if you have opcache enabled on your php.ini ?

Look up php.ini for opcache_enabled=1 and change the value to 0. Then restart your nginx/apache server and try again.

Sign up to request clarification or add additional context in comments.

5 Comments

I tried what you told me to. But still the same error.
Can you please paste your controller code (or pseudo code) here please? Maybe the use statements have something wrong in them.
Please try Use DB; and not use \DB
hmm.. very weird. Are you certain the error message says Class App\DB not found ? It should be Class 'App\Http\Controllers\DB' not found Just trying to find the root. What happens when you remove the line $user = Auth::user(); ? Could be a problem in your User model. Tough to tell without seeing the code there.
Okay, got it. It was actually calling a method from tickets model where DB was being using but not included hence the App\DB not found error. Got it. Thanks!
1

I got it working by adding the following to the top of my model:

use Illuminate\Support\Facades\DB;

Comments

-2

Adding this

use Illuminate\Support\Facades\DB; 

in the correct class should work. This answer just helped me. I was not being keen on which class should be modified. I was modifying a different class that does not require the DB class. I was adding it in DatabaseSeeder.php instead of the correct file needing it that has been UserSeeder.php

1 Comment

Please don't duplicate existing answers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.