How to run seeder under Users module - laravel

I need to run Laravel 6 application
and after running migrations I see that many tables are empty, including users.
In modules subdirectory I found files
/Modules/Users/Database/migrations/create_users_table.php
/Modules/Users/Database/seeders/UsersSeeder.php
Last file has default users
It was strange that running command
php artisan module:list
I see list of modules, but not Users module, as I expected
So I got error running in the root of my app:
php artisan module:seed Users
RuntimeException : Module [Users] does not exists.
I tried to run only this seeder, but again with error :
$ php artisan db:seed --class=/Modules/Users/Database/seeders/UsersSeeder
Illuminate\Contracts\Container\BindingResolutionException : Target class [/Modules/Users/Database/seeders/UsersSeeder] does not exist.
How can I run seeder under Users module(which I do not see in output of php artisan module:list)
?
Thanks!

If you use artisan command to make seeder
php artisan make:seeder UsersSeeder
You get the file in
database/seeds/UsersSeeder
Then you can call
php artisan db:seed --class=UsersSeeder
Instead of
php artisan db:seed --class=/Modules/Users/Database/seeders/UsersSeeder
Do not forget to run
composer dump-autoload
after first command

Related

Error when running database seeder through Heroku

I am unable to setup a packages folder structure with db:seed on Laravel framework.
I was able to deploy and migrate the regular Laravel project on heroku,
but I am having issue trying to use the class argument for the artisan db:seed command.
my folders structure:
root-dir/packages/clientname/projectname/app
root-dir/packages/clientname/projectname/config
root-dir/packages/clientname/projectname/database/seeders
root-dir/packages/clientname/projectname/resources
In composer I have autoload > psr-4 mapped to the packages seeders folder
"autoload": {
"psr-4": {
"ClientName\\ProjectName\\Database\\Seeders\\": "packages/clientname/projectname/database/seeders/"
}
},
running the following command
$ heroku run --app client-project php artisan db:seed --class=\\ClientName\\ProjectName\\Database\\Seeders\\DatabaseSeeder
returns an error
In Container.php line 811:
Target class [Database\Seeders\ClientNameProjectNameDatabaseSeedersDatabaseSeeder] does not exist.
Note the backslashes have been removed. How can I resolve this issue?
Checking out the source for this command, it appears there's a fair bit of escaping going on. I would suggest either double-escaping your backslashes like this:
heroku run --app client-project php artisan db:seed --class=\\\\ClientName\\\\ProjectName\\\\Database\\\\Seeders\\\\DatabaseSeeder
Or try some quoting around the command:
heroku run --app client-project 'php artisan db:seed --class=\\ClientName\\ProjectName\\Database\\Seeders\\DatabaseSeeder'

Laravel create migration file in specific folder

I've created Payment package for my laravel project
I want to make migration files inside migrations folder of my package. How can create it using artisan command?
I want something like
php artisan make:migration packages/Payment/src/Database/add_orderId_to_cart_payment_table
Use this command on the root folder:
create the only Migration file:
php artisan make:migration create_products_table --create=products
Create Migration, Model file:
php artisan make:model Product -m
For Create Migration,Model,Controller file:
php artisan make:model Product -mcr
If you want to do it manually then you may set --path as per your folder requirement.
php artisan make:migration filename --path=/app/database/migrations/relations
php artisan make:migration filename --path=/app/database/migrations/translations
If you want to migrate then:
php artisan migrate --path="/app/database/migrations/relations"
For specific directories:
php artisan make:migration create_users_table --path=/packages/Payment/src/Database
The new migration will be placed in your packages/Payment/src/Database directory.
For Running Migrations: php artisan migrate --path=/packages/Payment/src/Database
Generating a migration using the inbuilt framework feature:
php artisan make:migration create_users_table
The new migration will be placed in your database/migrations directory.
For Running Migrations: php artisan migrate
you need to publish them from your package to your migrations folder like this in your package service provider boot method:
$this->publishes([
__DIR__.'/Database/migrations/' => database_path('migrations'),
], 'migrations');
run this command php artisan vendor:publish --tag=migrations
and after that you can run php artisan migrate
You can run:
php artisan make:migration --help and see all the options available for you to use.
It seems you are making a package. It has nothing to do with where to create migrations, just create migrations in regular way, run your migrations and simply put them in your package/src/Database/migrations/yourMigrations_xxxxxx.php and in your package serivce provider in boot() method write this line
$this->loadMigrationsFrom(__DIR__ . '/Database/migrations');
laravel has a built in method loadMigrationsFrom('path/to/migrations') to pick up migrations, when someone installs this package and when he'll run php artisan migrate it'll run all the migrations whether they are in the user's Database/migrations or in your package's package/src/Database/migrations/2022_03_29_23424_create_countries_table_.php, all the migrations will run

How can I migrate my new migration file in Laravel?

sorry for silly question but I'm really facing problem. After migrating all the migration files. When I need another features for my project I have to make another table but When I'm going to migrate the new table using "php artisan migrate:rollback" or "php artisan migrate:rollback step=17" .... all the migration files are migrating this time too and I am losing all previous data. Then How can I migrate only the new one?
To migrate only new migrations is simple: php artisan migrate. The way the process works is that Laravel creates a table in your database called migrations. In that table are the names of the migrations that have been run already. If there are new migrations the above command will work.
You can read more about running migrations in the documentation.
To migrate any new migration created php artisan migrate is the artisan command.
To get the details of all commands php artisan is the artisan command.
To get the details of any specific command php artisan help command-name-here.
In the case of creating a new file, you only need to run php artisan migrate again.
If you want modify original migration file and migrate it again, you can use this package to migrate.
First, install package in your Laravel project:
composer require caloskao/migrate-specific
Register command at app/Console/Kernel.php :
protected $commands = [
\CalosKao\MigrateSpecific::class
];
Now, run this command to migrate your file
php artisan migrate:specific database/migrations/table.php

Lumen php artisan config:cache not found

I'm trying out the PHP micro Framework Lumen (from laravel).
When I set up Lumen and I try to use the php artisan config:cache command like in Laravel, I get this error :
[InvalidArgumentException]
There are no commands defined in the "config" namespace.
So I have problem when I try to deploy the files to server, so I have to change .env file to change the database username and password.
This makes me think config is not available in artisan
How can I add it to artisan ?
Yes, you can not use the php artisan config:cache with your Lumen project, because it is not available out of the box.
You can add it by adding this package (orumad/lumen-config-cache) to your project:
composer require orumad/lumen-config-cache
In lumen you have to add this configuration in bootstrap/app.php file
$app->configure('custom_config_file_name');
#example
$app->configure('custom_emails');
Then you can access like below:
config('filename.key_name');
#example
config('constants.email');
Lumen no need to config:cache.
You do not need to do anything after change ‍‍.env

Create Controller using artisan - InvalidArgumentException

I am new to laravel.
I have copied command to create controller from a book and I am trying to run that command:
php artisan Usercontroller:make users
I am getting error:
[InvalidArgumentException]
There are no commands defined in the "Usercontroller" namespace.
Did you mean this?
controller
Current working directory is project directory:
C:\wamp\www\laravel>
There is a typo in commmand,
you should use
php artisan controller:make users
Hope this helps.
Command should be
php artisan controller:make users

Resources