Class 'App\Http\Controllers\DB' not found and I also cannot use a new Model

PhpLaravelNamespacesModelsLaravel 5

Php Problem Overview


I have very basic problem. In L4 thes below methods worked out of the box, so now I am lost. Please help. A few days ago I started a Laravel 5.0 project. I have now fresh, clean installation.

Problem 1: When I try to get anything from database

$headquote = DB::table('quotation_texts')->find(176);

I get this:

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

Problem 2: Before I cloned the User.php Model, changed Class name to "Quotation". Below is the content of file Quotations.php put in App root folder:

<?php namespace App;

 use Illuminate\Database\Eloquent\Model;

 class Quotation extends Model  {

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'quotation_texts';
}

Any attempt to use the model

$headquote = Quotation::find(176);

ends up with this:

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

Any ideas how I could resolve the issue?

Php Solutions


Solution 1 - Php

The problem here are PHP namespaces. You need to learn how to use them. As your controller are in App\Http\Controllers namespace, if you refer any other class, you need to add leading backslash (or proper namespace) or add use statement at the beginning of file (before class definition).

So in your case you could use:

$headquote = \DB::table('quotation_texts')->find(176);
$headquote = \App\Quotation::find(176);

or add in your controller class use statement so the beginning of your controller class could look like this:

<?php

namespace App\Http\Controllers;

use DB;
use App\Quotation;

For more information about namespaces you could look at https://stackoverflow.com/questions/24607045/how-to-use-objects-from-other-namespaces-and-how-to-import-namespaces-in-php/24607087 or namespaces in PHP manual

Solution 2 - Php

Quick and dirty

use DB; 

OR

\DB::table...

Solution 3 - Php

Just add this top of your controller.

use DB;

Solution 4 - Php

Use the backslash before db on the header and you can use it then typically as you wrote it before.

Here is the example:

Use \DB;

Then inside your controller class you can use as you did before, like that ie :

$item = DB::table('items')->get();

Solution 5 - Php

Try Like this:

<?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    
    use DB;
   
    class UserController extends Controller
    {
        
    function  index(){
            
    $users = DB::table('users')->get();
    
    foreach ($users as $user)
    {
        var_dump($user->name);
    }
      
    }
  }

?>

Solution 6 - Php

There is problem in name spacing as in laravel 5.2.3

use DB;
use App\ApiModel; OR  use App\name of model; 

DB::table('tbl_users')->insert($users); 
 
OR

DB::table('table name')->insert($users);



model 

class ApiModel extends Model
	{
    	
    	protected $table='tbl_users';

}

Solution 7 - Php

I like to do this witch i think is cleaner :

1 - Add the model to namespace:

use App\Employee;

2 - then you can do :

$employees = Employee::get();

or maybe somthing like this:

$employee = Employee::where('name', 'John')->first();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPeterView Question on Stackoverflow
Solution 1 - PhpMarcin NabiałekView Answer on Stackoverflow
Solution 2 - PhpPedro LobitoView Answer on Stackoverflow
Solution 3 - PhpMamunur RashidView Answer on Stackoverflow
Solution 4 - PhpMarvin MustafaView Answer on Stackoverflow
Solution 5 - PhpranojanView Answer on Stackoverflow
Solution 6 - PhpvishalView Answer on Stackoverflow
Solution 7 - PhpinfantryView Answer on Stackoverflow