I have some command line scripts that I would like to modify to use Laravel's features (Eloquent etc).
How do I do that? I am aware that Laravel bootstraps from the index.html file. Is there any provision for running command-line apps / scripts?
php artisan make:command FancyCommand
.In /app/Console/Commands/FancyCommand.php
find a protected variable $signature
and change it's value to your preferred signature:
protected $signature = 'fancy:command';
Code in the handle()
method will be executed:
public function handle()
{
// Use Eloquent and other Laravel features...
echo 'Hello world';
}
Register your new command in the /app/Console/Kernel.php
by adding your command's class name to $commands
array.
protected $commands = [
// ...
Commands\FancyCommand::class,
];
Run your new command: php artisan fancy:command
.
Yes, you can use Artisan commands:
Artisan::command('my:command', function () {
// Here you can use Eloquent
$user = User::find(1);
// Or execute shell commands
$output = shell_exec('./script.sh var1 var2');
});
Then run it using
user@ubuntu: php artisan my:command
Check the docs: https://laravel.com/docs/5.3/artisan
You can also use the scheduler: https://laravel.com/docs/5.3/scheduling
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With