Transferring all Models in a directory

by Sam  

When you’re creating a simple project using Laravel framework, you might not need to organize your models in a specific directory. But, if you already have a lot of models to work on, it is recommended to put it in a specific folder.

By default, after creating a model through a terminal, all models go directly inside the app folder. Our objective in this article is to know how to transfer all models in a specific directory.

Upon installation of fresh laravel framework, we already have User.php model inside of app folder. Now, when transferring of all models into a folder, consider the steps below:

1. Create a Models folder inside app. So, you have now app\Models

2. Put the User.php inside app\Models folder. The structure will look like this:

3. Change namespace of User.php.

namespace App\Models; // change to this
class User extends Authenticatable
use Notifiable;

4. When you have controllers that use the Models, you also need to change the namespace. Example, from LoginController,

namespace App\Http\Controllers\Auth;
Use App\Models\User; // change to this
class LoginController extends Controller

5.  Open config/auth.php and change the providers setting

'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class, // change this

That’s all

6. Let’s say, we want to make People model. From the terminal, execute the command below:

$ php artisan make:model Models/People

If you have any comment, please feel free to add to the comment section below.

Leave a Reply

Your email address will not be published. Required fields are marked *

Need of a Website?

Maybe you're planning to build a landing page for your website. You might also have a plan to create a web application. I can offer you with an affordable but with good quality services. Don't hesitate to submit your specifications by clicking the button below.

Get Quote