How to call models in Laravel 5?

How to call models in Laravel 5?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Namespace Resolution and the "Models" Shortcut in Laravel 5

When working with Laravel 5, developers often encounter friction when trying to instantiate models that reside outside the default App\Models namespace. The specific scenario involves placing a model in a sub-namespace like app/Models/Blog while attempting to use a shorthand alias in your controller.

Consider a setup where you have created the directory structure app/Models/Blog and placed a file named Posts.php within it:

<?php namespace App\Models\Blog;

use Illuminate\Database\Eloquent\Model;

class Posts extends Model {
    protected $table = 'posts';
}

After running composer dump-autoload to refresh the class map, you might attempt to import this model in your controller using a relative alias:

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Models\Blog\Posts as Posts; // Intentional shortcut

class BlogController extends Controller {

    public function index()
    {
        $post = Posts::all()->toArray();
        dd($post);
    }
}

Executing this code results in a FatalErrorException: Class 'Models\Blog\Posts' not found. This error is not a mystery; it is a direct consequence of how PHP's autoloader resolves relative namespace imports.

The root cause lies in the import statement use Models\Blog\Posts as Posts;. Because your controller resides in the App\Http\Controllers namespace, PHP interprets this relative import by prepending your current namespace to the path specified. It effectively constructs the class name App\Models\Blog\Posts. However, the actual file is located at app/Models/Blog/Posts.php, which resolves to the fully qualified name App\Models\Blog\Posts.

While it appears correct at a glance, this specific shorthand creates ambiguity. The autoloader looks for a class named Models\Blog\Posts relative to the base App namespace in some contexts, but more critically, if you had intended the class to be found directly as Models\Blog\Posts (which would imply it lives in app/Models/Blog without the App prefix), the mismatch occurs because your model actually inherits from Illuminate\Database\Eloquent\Model under the App\Models\Blog namespace.

The critical issue here is that the alias use Models\Blog\Posts as Posts; tells PHP to look for a class defined in the global namespace or relative to the script's base, but it fails to account for the full depth of your application's root namespace when combined with the specific file location. The error message explicitly states that Models\Blog\Posts is missing, confirming that the resolution path constructed by the relative import does not match the actual class definition registered in the autoloader.

To resolve this, you must use a fully qualified namespace import that matches the exact path defined in the model file. You need to include the App prefix in the import statement:

use App\Models\Blog\Posts as Posts;

By using App\Models\Blog\Posts, you ensure that the autoloader correctly maps the string to the file at app/Models/Blog/Posts.php. This approach removes ambiguity and guarantees that the class is found regardless of where the controller resides.

This situation highlights a common pitfall in Laravel 5 development: assuming that relative namespace shortcuts will always work intuitively. While Laravel's service provider registration allows for App::make('post') or similar magic methods to find models by their string name (e.g., 'App\Models\Blog\Posts'), direct instantiation via the class alias requires precise namespace matching.

If you rely on string-based model retrieval, ensure that the string key used in App::make() matches the full namespace including the App root. For example:

$post = App::make('App\Models\Blog\Posts');

While this works, direct imports are generally preferred for readability and type safety. Always verify that your composer dump-autoload has been run after creating or moving model files to ensure the class map is up to date. Neglecting this step often leads to the exact "class not found" errors described above, forcing you to troubleshoot namespace paths rather than application logic.

For further insights on managing models and namespaces in complex Laravel applications, resources like https://laravelcompany.com can provide additional context on best practices for architecture and dependency management.

Our network