Using Artisan::call() to run package migrations - php

I need to migrate my database schema for some unit tests I'm writing, and one of these migrations is included in a package. Normally, from the command line, I'd run this command:
php artisan migrate --package=tappleby/laravel-auth-token
And to run my own migrations in code I'd do:
Artisan::call('migrate');
However, I can't seem to get Artisan to run package migrations from inside code. I've tried this:
Artisan::call('migrate --package=tappleby/laravel-auth-token');
but that results in an unknown command error. I've also tried these:
Artisan::call('migrate', '--package=tappleby/laravel-auth-token');
Artisan::call('migrate', ['--package=tappleby/laravel-auth-token']);
Artisan::call('migrate', ['package=tappleby/laravel-auth-token']);
None of the above works. What's the correct way of running package migrations in my code?

I believe the correct syntax uses an associative array for the command parameters, where the item key is the name of the parameter and the item value is the value of the parameter. This should work in your case:
Artisan::call('migrate', ['--package' => 'tappleby/laravel-auth-token']);

I did it with --path:
Artisan::call('migrate', ['--path' => 'vendor/systeminc/laravel-admin/src/database/migrations']);

Related

Laravel cannot find public dir

I worked on a team and then clone the laravel/php codes from our repository. When I serve the laravel on localhost, it cannot find all the file inside the public directory and throw an error on the terminal:
[404]: GET /public/css/style.css - No such file or directory
Many answers to similar issues advise me to change the codes in the blade.php file. The problem is that I can't edit the blade.php because it works fine on other team members even though we have the same ubuntu, PHP, and laravel version and .env file.
What I have tried:
Run composer install and composer update
Try different browsers such as firefox and chromium-based browsers
Run php artisan storage:link
Restart apache2
Run npm install and npm run dev
I think there is some package or something missing.
List of paths I get from dd($__data):
"path" => "/project-directory/project-name/app"
"path.base" => "/project-directory/project-name"
"path.lang" => "/project-directory/project-name/resources/lang"
"path.config" => "/project-directory/project-name/config"
"path.public" => "/project-directory/project-name/public"
"path.storage" => "/project-directory/project-name/storage"
"path.database" => "/project-directory/project-name/database"
"path.resources" => "/project-directory/project-name/resources"
"path.bootstrap" => "/project-directory/project-name/bootstrap"
Have you tried?
php artisan storage:link
You need first run
php artisan storage:link
For more details check https://laravel.com/docs/8.x/filesystem
Then you must be check your .env values.
Is correctly APP_URL=
Also in your exception is strange
/public/.../...
Reason is wrong configuration.
First check your .env values like
APP_URL=...
FILESYSTEM_DRIVER=public
then check config/filesystems.php

Laravel - DB Seeder command not working as expected

I've created a custom command functionality for my project, but there is something strange happening at the moment.
First i thought it might had something to do with the composer dump-autoload but after adding the exec command it still didn't work.
I do have the
seeds as autoload in my composer.json
$this->className = "MyDbSeeder"
$this->call('make:seeder', ['name' => $this->className]);
exec("composer dump-autoload");
sleep(5);
$this->call('db:seed', ['--class' => $this->className]);
/* Response */
// -- ReflectionException : Class MyDbSeeder does not exist --
/* But when i'm running the exactly same command but not through the `$this->call()` function it works perfectly fine. */
exec("php artisan db:seed --class {$this->className}", $output);
/* Response */
// -- "Database seeding completed successfully." --
Of course it does work with the exec command, but i don't feel like this is the solution that i need to deliver.
Anyone knows why the regular laravel command ain't working?

env values from Linux not loaded by Laravel config app

Some time ago I asked about setting env settings and how to properly use them. I was quickly pointed to a comparable question and I found out that indeed it was bad practice to use env('KEY') throughout your code.
So now I am in the process of migrating my env settings to config/app.php.
However, if I play with Tinker, the env variables from Linux are not loaded by Laravel. For instance, if I place:
'test' => 'testing123',
within the config/app.php
and do a
sudo php artisan config:cache
and employ Tinker
config('app.test');
=> "testing123"
So that seems to work. However, if I place the following
'test' => env('DB_PORT'),
and do a
sudo php artisan config:cache
and test this with tinker:
config('app.test');
=> null
But when I am in the console and use:
env|grep DB_PORT
I see the right value for the DB_PORT key. I am supplying these in AWS frontend, these properties are then passed in the application as environment properties.
Anyone any idea why these are not imported/loaded correctly?
php artisan config:clear
Or you can just manually delete bootstrap/config.php, which is what artisan does after all.
See: vendor\laravel\framework\src\Illuminate\Foundation\Console\ConfigClearCommand.php

php artisan migrate:refresh | ERROR [Laravel5]

I use Laravel 5.
when i try to refresh the migration,
INPUT:
php artisan make:migrate
OUTPUT :
[Symfony\Component\Debug\Exception\FatalErrorException]
Class '****' not found
Even i remove that classes also.
Just run
composer dump-autoload
The Class "Todo", which I created record is exist in "Migration" table. Command working when I remove that record.

Laravel 4 migrate:rollback with --path on artisan CLI

I'm having some roadblock on Laravel 4.
Since I can't make artisan:migrate generate migrations from inner folders of app/database/migrations (ex: app/database/migrations/app1)
I have this on my custom command app:migrate
/* default path */
$this->call('migrate');
/* custom path */
$this->call('migrate', array('--path' => 'app/database/migrations/app1'));
but i also want an app:refresh command which will rollback all the migrations from the custom path then from the default path.. then re migrate and seed everything just like what migrate:refresh --seed does
how do i reverse this? calling:
$this->call('migrate:rollback', array('--path' => 'app/database/migrations/app1'));
will produce an error saying
[InvalidArgumentException]
The "--path" option does not exist.
can somebody help please.
thanks!
All you have to do is make sure your migration classes can be autoloaded. The easiest way to do so is to add the path to the folder you're keeping them to composer.json's autoload.classmap:
...
"autoload": {
"classmap": [
...
"app/database/migrations/app1",
]
},
You should re-run migrate with "--path" option manually for this case.

Categories