What I've done
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 an 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.
What I want
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.
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.
You need to modify your
Project/config/auth.php
file on
'model'=> AppName\Models\User::class,
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:
Move User.php from App\ to App\Models folder
Update the namespace of User.php from namespace App; to namespace App\Models;
Find and replace in your project (global search) for: use App\User.php and replace all results with use App\Models\User.php
Check your application and run tests if you have them.
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
After manually deleting a Trait file that was no longer needed by the compiler I see that the compiler is looking in the Controller folder instead of the Traits folder.
I have tried composer dump-autoload, php artisan clear-compiled, php artisan /clear-cache and php artisan optimize. How can I fix this problem?
After manually deleting a Trait file that was no longer needed by the compiler I see that the compiler is looking in the Controller folder instead of the Traits folder.
I guess this means you moved the Trait file into another directory?
Since composer usually is configured to use the PSR-4 autoloading this would require you to also change the namespace of the move Trait.
I.e. trying to load App\Http\Controllers\CommonTrait the autoloader is looking for the Trait in the src/Http/Controllers/CommonTrait.php file. If you moved that file to say src/Http/Controllers/Traits/CommonTrait.php you have to change the namespace of the Trait to
namespace App\Http\Controllers\Traits;
and import it for usage as
use App\Http\Controllers\Traits\CommonTrait;
The answer to my problem is to make sure that nothing in your code is using the trait. I found a controller that was still using the trait. Big newbie mistake. Maybe it's time for this old sea-dog to ...
I have an Yii2 web application with 50+ model class files that are located in /models directory.
Now I want to run some console scripts from /console/controllers/MyController.php using these models but get class app\models\ModelName not found error, despite of use app\models\ModelName at the top.
If I copy a model file to /console/models/ModelName.php or /common/models/ModelName.php (and make change in use) it works alright. Is there any option to use models from /model or should I refactor the application so that both web and console use model files from /common/models
If you are using yii2-advanced build take in mind that #app alias is set each time depending on what part of application you are using.
If you are making a call from frontend, #app will be equal to /path/to/project-root/frontend.
If from backend - /path/to/project-root/backend.
console - /path/to/project-root/console
You may add custom alias in /common/config/bootstrap.php, to make your classes available from root.
For example try to add Yii::setAlias('#root', dirname(dirname(__DIR__))); to /common/config/bootstrap.php and set namespace to root/models
Note: if you will try add #app to bootstrap.php, it will be automatically reassigned by framework.
Note 2: You may check how yii2 autoloader works in BaseYii.php
Working on a inherited Laravel Spark project that contained two User models.
One is the standard Spark model inside the App directory but the other is inside App/Models. I have combined the two models and updated the auth.php to reference the User model inside the Models directory but composer dump-autoload is saying it cannot find the App/User model.
How can I tell the autoloader that the User model is not there any more but
instead in the Models directory?
Edit:
I have changed the namespace to App/Models but still receive the error:
class_parents(): Class App\User does not exist and could not be loaded
In my terminal when running dump-autload
Second Edit:
Fixed, didn't realise the namespace was referenced so much. Did a find and replace on App\User and sorted the issue.
A standard Laravel installation will work by simply changing the namespace as others have mentioned; however Laravel Spark references the User and Team models, therefore a namespace change alone will not work.
You shouldn't edit any files inside the vendor/laravel/spark-aurelius (aurelius codename will vary depending on your version) as these changes are not tracked.
With Spark, you should add the following lines into your app/Providers/SparkServiceProvider.php:
public function register()
{
Spark::useUserModel('App\Models\User');
Spark::useTeamModel('App\Models\Team');
}
You can set your own custom App\Models directory, rather than using the above example.
Finally you will need to update any references you make to your models, e.g. update controllers from use App\User to use App\Models\User.
Source: Laravel Spark 6.0 Customization
5th Jan 2020 Update: Remember to also update STRIPE_MODEL and BRAINTREE_MODEL values in your .env to your new namespace.
Laravel Spark 9.0 removes Braintree support, therefore you only need to update CASHIER_MODEL in 9.0.
21st Dec 2020 Update: Laravel 8.x now keeps all models in the app\Models directory by default. Even if you're on an older version of Laravel (e.g. 6.x) but are using Laravel Spark 11, then you do not need to do any of the above. Laravel Spark 11 will assume your models live in app\Models.
You need to change User model namespace:
namespace App\Models;
Try renaming the namespace. :)
namespace App\Models;
change namespace, and use like this
namespace App\Models;
So I'm trying to create a custom console command in Laravel 5.1 which does some helpful function for my project. I can do this fine when putting the console command in a file located at the base commands folder but not when I just to add a subdirectory.
'App\Console\Commands\SomeCustomCommandThatWorks',
'App\Console\Commands\MySubNameSpace\CustomCommandThatFails',
So how do I add my command like MySubNameSpace\Command?
Namespace doesn't appear to have any effect on this. The namespace of the command could be App\Console\MySubNameSpace\MyCommand or App\Console\MyCommand both fail if the file is located at 'App\Console\Commands\MySubNameSpace\MyCommand'. The file also fails if located at 'App\Console\Commands\MyCommand' with namespace App\Console\MySubNameSpace\MyCommand.
Right now I get this error.
Class App\Console\Commands\DeletePhantomServers does not exist
I have tried running composer dumpautoload but to no success.
Any help would be appreciated.
According to autoloading standards, none of the combinations you have above will work. The namespace needs to be set according to the directory. So if you have the command in this folder
Console\Commands\MySubNameSpace\MyCommand
your namespace has to be
App\Console\Commands\MySubNameSpace\MyCommand
I must be tired. After reading Michael's answer I looked at the namespaces and realised I was just simply writing them wrong.
I was putting
namespace App\Console\Commands\MySubNameSpace\MyCommand;
class MyCommand ...
Where I needed to put
namespace App\Console\Commands\MySubNameSpace;
class MyCommand ...
Thanks for the help people :)
I am running the Yii2 framework locally, and I want to reuse an model I created in a earlier project.
So I copy the file TestForm.php to the models directory, change the namespaces from namespace backend\models to namespace app\models and try to create an object from it with:
$model = new \app\models\TestForm;
Which gives me
Unable to find 'app\models\TestForm' in file: /var/www/html/operators/basic/models/TestForm.php. Namespace missing?
Which is weird because the namespace is correct.
However, if I create the file TestForm.php myself and copy the contents of the older file over, everything works fine.
What's going on?
(I use ubuntu 15.04)
I think in your /models/TestForm.php you don't have specified the correct namespace eg :
namespace basic\models;