Home > Enterprise >  changing the path of models into a models directory
changing the path of models into a models directory

Time:11-13

I used the command "php artisan make:model User" without mentioning model folder as the command should have been "php artisan make:model Models\User". I am a amateur in terms of laravel and just started php and laravel last month and made a whole multi vendor project around it without the models being in models folder. Now, I'm just trying to structure my code in a cleaner manner and facing errors as have to change the path of models wherever it is mentioned. I tried doing them but still can't get rid of the errors.

CodePudding user response:

Latest version of Laravel, by default, file is already created inside the Models folder. If you have a model outside folder and what you want is to incorporate it there, the best thing to do is to change the built-in namespace at the top of everything. For example:

namespace App\Models;

If you want is to completely change the path, first create the model and then move folder manually. Finally, in namespace mention it:

namespace FolderA\FolderB\FolderC;.

Although it is always better to follow the default Laravel structure, is cleaner.

CodePudding user response:

You need to modify your Project/config/auth.php file on 'model'=> AppName\Models\User::class,

CodePudding user response:

The best thing to do is to move 1 model at a time and replace all of its import namespaces. Test your application if everything works and then continue with the next model and so on.

Example:

  1. Move User.php from App\ to App\Models folder
  2. Update the namespace of User.php from namespace App; to namespace App\Models;
  3. Find and replace in your project (global search) for: use App\User.php and replace all results with use App\Models\User.php
  4. Check your application and run tests if you have them.
  5. If everything is ok, you can continue moving the next model to the Model/ folder and repeat step 1 to 5

For the model User.php: As @saeed mentioned, update your config/auth.php if needed.

Note that IDE's these days can do the most work for you, like PhpStorm can do all the find & replaces for you when you pressing F6 on the User.php file in the tree and update its path.

  • Related