0

I made a project and it was working absolutely fine on localhost. But when I made it online then it is showing following error in networking tab of browser while I am trying to login to my project.

{message: "Class 'App\Models\user' not found", exception: "Error",…} 

All codes are same, files and directory structure are same. .env file has been updated. But still getting this error.

Note: During search I found this post (and many other articles on different sites) but it is not showing any working solution.

Laravel 7.11.0 Class '\App\Models\User' not found

4
  • 5
    user should be User. If your local is windows and your production is Linux this will cause a problem because the Linux filesystem is case sensitive while the windows one is not. Commented Jul 23, 2021 at 4:44
  • Strange that is working locally but not remotely... Also, is it User or user ? That is pretty important. Show us the namespace of your user model. Commented Jul 23, 2021 at 4:44
  • @apokryfos OMG, didn't know it is also a thing. YES, you are right, I am using windows in localhost. I tried your solution and it worked.. Commented Jul 23, 2021 at 4:47
  • 1
    @apokryfos add this as answer .This might help some one in future. Commented Jul 23, 2021 at 4:50

2 Answers 2

2

Laravel uses composer to load classes and configures the autoloader as a PSR-4 autoloader. The PSR-4 spec states that

  1. All class names MUST be referenced in a case-sensitive fashion.

and

The terminating class name corresponds to a file name ending in .php. The file name MUST match the case of the terminating class name.

In the default configuration Laravel specifies that the root namespace is App and is situated in the app folder (ironically Laravel did not make this case sensitive but composer allows this for the root namespace). So for example if you want to load App\Models\user then composer will look for the file app/Models/user.php. Note that in the default Laravel installation the file is actually named User.php.

Now I do think there is a bug in the composer autoloader or a limitation in Windows in that it only checks if the file exists and doesn't check if the file also matches the case. Since Windows is a case insensitive filesystem when composer asks it if app/Models/user.php exists then Windows will respond that it does, even if the casing is wrong. However Linux is a case sensitive filesystem therefore if you ask a Linux OS if app/Models/user.php exists it will respond that it does not. This will create a discrepancy between Windows and Linux if you're not careful.

I'm not sure if this is a composer bug or configuration or caching issue but the best course of action is to always match the case of your file names in your class names and in the way you refer to them.

Sign up to request clarification or add additional context in comments.

Comments

0

If you are moving files to another place then you can make Composer dump-autoload if it's the you can first make composer update then composer dump-autoload it's solved with me.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.