Laravel - Class "App\Http\Controllers\DB" not found - Solved

By Hardik Savani April 16, 2024 Category : Laravel

In this post, we will learn laravel class db not found. this example will help you laravel class 'app http controllers db' not found. I would like to share with you Laravel Class "App\Http\Controllers\DB" not found. I would like to share with you laravel class app http controllers db not found.

You can solve 'Class "App\Http\Controllers\DB" not found' issue in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.

A few days ago I was working on my laravel app and I simply get users with email and name concat using DB class. When I run the project then I found 'Class "App\Http\Controllers\DB" not found' error. you can see bellow screenshot as well.

Issue:

After some research, I found that If you are using the "DB" class in Controller, Middleware, or blade file then you must have used class on top.

so let's see bellow solution and example code here:

Solution:

You must need to add "use DB;" on top of controller, middleware, command, event or blade files. Let's see bellow:

use DB;

Example:

You can see controller file code, how to use it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

use DB;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index(Request $request)

{

$users = User::select(

'id',

'name',

'email',

DB::raw("CONCAT(name, ' - ', email) as name_email")

)->get();

return view('users');

}

}

I hope it can help you...

Tags :
Shares