Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move Laravel 5 Eloquent models into its own directory

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?

like image 533
Margus Pala Avatar asked Mar 14 '15 17:03

Margus Pala


People also ask

What directory are Laravel models stored?

Models in Laravel 5.5 are created inside the app folder. Models are mostly used to interact with the database using Eloquent ORM. Eloquent provides simple ActiveRecord implementations for database interaction.

What is Laravel eloquent 5?

Eloquent is an object relational mapper (ORM) that is included by default within the Laravel framework. An ORM is software that facilitates handling database records by representing data as objects, working as a layer of abstraction on top of the database engine used to store an application's data.

Can I use eloquent ORM without Laravel?

Yes you can. A while ago Dan Horrigan released a package called Capsule for Laravel 4 which allowed Eloquent to be used independently and with minimal setup. The package itself has been merged with the L4 core so you no longer need to use the package.


1 Answers

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!

like image 81
Saint Play Avatar answered Oct 07 '22 01:10

Saint Play