42

Laravel 5 has ORM models by default in app folder. I want to move these into app/models. When I do that then these classes are not found anymore.

How to make Laravel find the Eloquent ORM models from app/models?

Margus Pala
  • 8,433
  • 8
  • 42
  • 52

5 Answers5

74

Solution for Laravel 5.6+


1. Move the files to the new directory

Say you want to move the models to app/Models

2. Change the namespace of the models

For each model change :

namespace App;

to

namespace App\Models;

3. Change the references in other files

Check these files and search especially app\User

  • app/Http/Controllers/Auth/RegisterController.php
  • config/auth.php
  • config/services.php
  • database/factories/ModelFactory.php
  • database/factories/UserFactory.php
  • Your Controllers

And change App/ModelExample to App/Models/ModelExample

4. Autoload files

Run composer dump-autoload

5. Congratulations!

Saint Play
  • 1,083
  • 10
  • 12
  • Just updated to 5.4 and was struggling a bit. Excellent instructions. Thanks. – DavidHyogo Jul 06 '17 at 09:30
  • @SaintPlay I tried above steps but it did not seem to work properly. Like few pages did work when I use models inside a controller. but some of the pages still throw `App\ModelName` Error. the model does not seem to reflect the change. I couple of times did dump-autoload and also did cache:clear, view:clear. How can I debug these issue? – Murlidhar Fichadia Feb 19 '18 at 13:03
  • @SaintPlay I get these when I do composer dump-autoload `Warning: Ambiguous class resolution, "App\Models\Company" was found in both "$baseDir . '/app/Models/Company.php" and "C:/Users/murli/laravel/Bookings/app/models/Company.php", the fi rst will be used. ` – Murlidhar Fichadia Feb 19 '18 at 13:06
  • @MurlidharFichadia what version of laravel are you using? – Saint Play Feb 21 '18 at 12:35
  • @SaintPlay I am using Laravel 5.6, most recent one. I had to revert everything back to default. I failed to move all the model into models folder. I did php artisan confg:clear, cache:clear, view:clear – Murlidhar Fichadia Feb 21 '18 at 12:37
  • @MurlidharFichadia Seems like a case error. Make sure your folder and namespaces has the same case. By the way, I just updated the instructions to Laravel 5.6 – Saint Play Feb 25 '18 at 01:55
  • You can also avoid having to change the namespaces and usages by just changing this in composer.json: `"psr-4": { "App\\": "app/" }` to this: `"psr-4": { "App\\": ["app/", "app/models/"] }`. This way, classes both in `/app` AND in `/app/models` will be in the `App` namespace (it's up to you to put models inside "models" and other stuff outside of it). I'm not sure it's something I'd recommend, as you can run into ambiguities and conflicts, but you should be fine as long as you don't have similar subpaths both in /app and in /app/models, and you don't actually use a "Models" namespace – matteo Sep 02 '18 at 16:01
37

Just create a folder named Models and move your files into it. After that, change the namespace of the models from just App to App\Models (update the usages as well) and run composer dump-autoload.

lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
  • 1
    `composer dump-autoload` was missing and now it works in combination with composer.json autoload. I did not want to start changing classes namespaces. – Margus Pala Mar 14 '15 at 19:38
  • 6
    Don't forget to change the UserModel for Authentication if using it. See also here: http://laravel-recipes.com/recipes/11/changing-your-authentication-model – algorhythm Mar 19 '15 at 06:46
16

Add "app/models" to composer.json's classmap autoload section

"autoload": {
    "classmap": [
        "database",
        "app/models"
    ]
}
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
Dallin
  • 1,075
  • 13
  • 28
  • I tried it before and it did not work. Now it works thanks to lukasgeiter suggestion to use `composer dump-autoload` that made the classes to be found. – Margus Pala Mar 14 '15 at 19:36
  • Thanks, this advice composer update is missing from other solutions I was reading, but it is the one that does the trick for me! – Maxcot Jul 30 '16 at 03:54
  • @Dallin I did update composer json as you suggested. also I followed SaintPlay answer. but it did not seem to work properly. Like few pages did work when I use models inside a controller. but some of the pages still throw App\ModelName Error. the model does not seem to reflect the change. I couple of times did dump-autoload and also did cache:clear, view:clear. How can I debug these issue? – Murlidhar Fichadia Feb 19 '18 at 13:04
  • 1
    @Dallin I get these when I do composer dump-autoload `Warning: Ambiguous class resolution, "App\Models\Company" was found in both "$baseDir . '/app/Models/Company.php" and "C:/Users/murli/laravel/Bookings/app/models/Company.php", the fi rst will be used. ` – Murlidhar Fichadia Feb 19 '18 at 13:06
  • 3
    I have had this exact same issue, and found the solution lies in making sure the references are 100% correct. Changing "app/models" to "app/Models" fixed it for me. – Maxcot Jun 03 '18 at 08:51
3

Afer moving all files in the models folder. What you need to do is run:

composer dump-autoload

This worked for me. :)

  • Does this works without adding App\Models folders to composer.json's classmap ? If this, how is it posible? Anyway, I thinks it's needed to update namespace of each model, right?. – realtebo Jul 24 '18 at 10:13
  • 1
    Have tried it with other projects of which it should work without adding classmap to composer.json. For more info checkout this link https://medium.com/@codingcave/organizing-your-laravel-models-6b327db182f9 – Fredrick Ochieng Jul 24 '18 at 11:55
1

After doing @Saint Play steps. I had to do php artisan config:clear. Then it worked !

Rakesh K
  • 1,290
  • 1
  • 16
  • 43