Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: How do I run a console command inside a migration?

I created a Laravel console command in routes/console.php. I would like to run this command from a migration? How can I do it?

I would like to avoid using the PHP's exec() function because it's unpredictable where the path of my laravel app is going to be and which OS will be run on.

I am using Laravel 5.x

like image 354
lara Avatar asked Jul 20 '18 08:07

lara


People also ask

What is migrate command in Laravel?

Roll Back & Migrate Using A Single CommandThe migrate:refresh command will roll back all of your migrations and then execute the migrate command. This command effectively re-creates your entire database: php artisan migrate:refresh. # Refresh the database and run all database seeds... php artisan migrate:refresh -- ...

How do I run a migration file in Laravel?

IF you want to re-migrate all the database, you can simply do: php artisan migrate:refresh . IF you want to make sure your database to be clean with your latest changes, you can drop your entire database tables and do php artisan migrate again. Also, you can try php artisan migrate --seed if you have any seeder.


1 Answers

You can use

Artisan::call('email:send');

From docs with command parameters

$exitCode = Artisan::call('email:send', [
    'user' => 1, '--queue' => 'default'
]);

https://laravel.com/docs/5.6/artisan#programmatically-executing-commands

like image 165
Marcus Avatar answered Oct 23 '22 02:10

Marcus