无法访问自定义类文件laravel 5中的模型和帮助程序类

I have created a common class in app/Classes/Common.php but whenever i try to access a model in a class function.

$new_booking_request = BookingRequest::where('host_id','=',Auth::id())

I am getting this error

Class 'App\Models\BookingRequest' not found

Even other classes like Auth, URL and Cookie are not working. Is there a way to bring all classes in my Common class scope?

In Laravel 5 everything is namespaced, make sure your class has a proper namespace and that you are calling it using that same namespace you specified.

To include classes in another class make sure that you use the use keyword to import the necessary classes on top of your class definition. Also you can call the class globally with the \. Ex: \Auth, \URL and \Cookie

For the namespace in L5 here is a quick example:

<?php namespace App\Models;

class BookingRequest {
  // class definition  
}

then when trying to call that class, either call the full namespace path of the function, or include the function.

<?php

class HomeController extends Controller {

  public function index()
  {
    $newBookingRequest = App\Models\BookingRequest::where('host_id','=',Auth::id());
  }

}

OR

<?php namespace App\Controllers;

use App\Models\BookingRequest; // Include the class

class HomeController extends Controller {

  public function index()
  {
    $newBookingRequest = BookingRequest::where('host_id','=',Auth::id());
  }

}

PS:

Please use camelCase when defining class attributes and methods as this helps for a better code-styling and naming conventions when using the L5 framework.

You get this issue when your namespace is wrong you or you forgot to namespace.

Since common.php is inside App/Classes, inside Common.php do somethng like this:

<?php namespace App\Classes;

use View, Auth, URL; 

class Common {
    //class methods
}

Also ensure your model class has the correct namespace, if BookingRequest.php is located inside App\Models then inside BookingRequest.php do this:

<?php namespace App\Models;

    BookingRequest extends \Eloquent {
        //other definitions
    }

Then if you wish to use BookingRequest.php outside its namespace or in another namespace like so:

<?php namespace App\Classes;

use App\Models\BookingRequest;

use View, Auth, URL; 

    class Common {
        //class methods
    }