Error when running database seeder through Heroku - laravel

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'

Related

How to run seeder under Users module

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

Problem with executing laravel migrations on Elastic Beanstalk

I deployed my laravel application using Elastic beanstalk, and I need to execute php artisan:migrate command on the remote database.
Based on Maximilian's tutorial I created init.config file inside .ebextensions with contents:
container_commands:
01initdb:
command: "php artisan migrate"
The status of the deployment is Healthy, but it didn't create any table!
any clues, please?
run php artisan config:cache then run migrate command
As you did not include any relevant logs. I am guessing you need to run your migration within the staging folder.
An example of how you can run migrations:
04_run_migrations:
command: "php artisan migrate --force"
cwd: "/var/app/staging"
leader_only: true
--force is needed cause php artisan migrate asks you if you are sure to run it on a production environment
leader_only is needed if you use horizontal scaling.
Source:
https://github.com/rennokki/laravel-aws-eb/blob/master/.ebextensions/01_deploy.config

Access Forbidden on heroku while deploying laravel app

I am trying to host a laravel application oh heroku. But i'm gettin the access forbidden message. I have created the Procfile and placed the following code
web: vendor/bin/heroku-php-apache2 public/
I have also placed the following code in my composer.json
"php artisan clear-compiled",
"php artisan optimize",
"chmod -R 777 public/"
I also have checked if the htaccess file exist in public directory.
I'm still getting the error. Please help me with this some one
Make sure your Procfile is inside the root directory of your project (not inside public/), and that it contains web: vendor/bin/heroku-php-apache2 public

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

How to automatically run the migrations in a Laravel package?

I just installed Cartalyst's Sentry 2 in a Laravel 4 application but I found out that I have to run that package's migrations separately by specifying --package=cartalyst/sentry, which makes automatic deployment impossible.
Is there a way to run php artisan migrate and have it run Sentry's migrations as well?
After Laravel 5 there is a better way to solve this:
Create your migrations in package's
/database/migrations folder
.
After that, in package's service provider create a boot method, referencing the migrations folder
public function boot()
{
$this->loadMigrationsFrom(__DIR__ .
'/database/migrations');
}
Now, on the top level folder (the main app, which required this package), you can run the default migrate command ( php artisan migrate ) and it will automatically find the packages migrations througth the loadMigrationsFrom method
ANSWER COPIED FROM:
http://voerro.com/en/tutorials/r/developing-and-distributing-laravel-5-packages/3
What I usually do in a scenario like this is publish the package migrations though the command:
php artisan migrate:publish vendor/package
This copies the migration files from any given package to your migrations folder.
I created a composer script to replace php artisan migrate ...... The script runs my migrations and the vendor's migrations everything at once.
My composer scripts is
"scripts": {
"migrate": [
"php artisan migrate --env=$LARAVEL_ENV",
"php artisan migrate --package=\"cartalyst/sentry\" --env=$LARAVEL_ENV",
"php artisan migrate --package=\"mrjuliuss/syntara\" --env=$LARAVEL_ENV",
"php artisan migrate --package=\"filmoteca/static-pages\" --env=$LARAVEL_ENV"
]
}
Then you can run the migration with LARAVEL_ENV=prod composer run-script migrate
To pass parameter to the script I use environment variables. In the previous example I set the environment variable LARAVEL_ENV to prod so the migration use the production database connection.
You can always create a alias in your local machine to short the command. For example alias migrate="LARAVEL_ENV=local composer run-script migrate"
I think this approach is good because when you are going to add a new package to your composer.json and this package has a migration you add the package and the package's migration in the same file. So, you do not forget add/remove the migration of a package.
This is a complete composer.json with the script
The 3rd party package must declare a Service Provider, that declare migrations.
As you can see at https://github.com/laravel/passport/blob/9.x/src/PassportServiceProvider.php#L80:
protected function registerMigrations()
{
if (Passport::$runsMigrations && ! config('passport.client_uuids')) {
return $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
}
}

Resources