How to Resolve Laravel Model Not Found Issue
Thank you for your continued support.
This article contains advertisements that help fund our operations.
Table Of Contents
⇨ Click here for the table of contents for Laravel articles
In Laravel, models are often not found.
In terms of errors
Symfony\Component\Debug\Exception\FatalThrowableErrorClass 'App\Http\Controllers\Post' not found
Symfony\Component\Debug\Exception\FatalThrowableErrorClass 'App\Post' not found
These are the kinds of errors you might encounter.
This is an error that everyone experiences, like a traditional performance.
Please try the solutions I will introduce one by one.
【Related Article】How to Debug in Laravel
Why Does the Error Occur?
- There is no declaration stating "I will use this model (class)".
- The model (class) has not been loaded.
- The file might not exist.
These are the three main reasons, I believe.
Of course, there might be exceptions in programming, but at present, aren't these three reasons the main causes of the error?
Add "use App\Post"
This will resolve reason 1
At the top of the controller, add the model using the use
keyword.
<?php
namespace App\Http\Controllers\Client;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
// Add the following
use App\Post;
Pay attention to the hierarchy when specifying where the model is located.
use App\Models\Post;
is also a possibility.
The "use App\Post" statement is incorrect
If the class' position (file's position) is incorrect, it won't work, so you need to specify the correct hierarchy.
Check if it is correctly written at the right hierarchy.
The class has not been cached
This is a bit more complex, but classes such as models and controllers are cached with their locations.
The cache holds information like "There is a class with this name in this location".
When that information is missing from the cache, this error occurs.
For example, when a file is manually copied or duplicated, there is no cache, which leads to an error.
In such cases, running the command:
composer dump-autoload
usually resolves the cache issue.
The namespace is incorrect
When you copy, move, or create your own file, there might be cases where the directory where it is saved and the namespace are incorrect.
Check the namespace.
namespace App;
If it is incorrect, edit the namespace and run the command:
composer dump-autoload
The reason for running this command is the same as the issue of uncached classes.
There might be a possibility that the file itself is missing
Surprisingly, there might be an error where the file itself is missing.
There could be an error occurring when running a command, and it might not have been generated.
Best Practice to Avoid these Errors
I recommend:
Always generate files using commands
If generating a model, for example,
php artisan make:model Models/Post
This command will create the model.
By generating files using commands, the information about the file will be properly cached, reducing the chances of accidents.
If you want to copy the contents, it is advised to generate the file using commands and then copy and paste the content.
Summary
That's all.
This error might become familiar to you, so you will eventually get along with it.
If you were able to solve this issue with this article, please share it on Twitter.
For feedback or complaints, please contact me via Twitter DM.
That's it!
Popular Articles
Deploying a PHP7.4 + Laravel6 Project to AWS EC2
Implementing Breadcrumbs in Laravel with laravel-breadcrumbs