laravel 4.2 artisan command:name with arguments - php

I have created an artisan command called sendUnreadNotifications this triggers the system to send users emails if they have unread notifications. Eventually this will be run via a cron job, and a user can either have hourly updates or daily updates.
For this reason I am wanting to send an argument with my command, something like this,
php artisan command:name sendUnreadNotifications H --env=local
However running this, I get the following error,
[RuntimeException]
Too many arguments.
My code, looks like this,
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class sendUnreadNotifications extends Command {
/**
* The console command name.
*
* #var string
*/
protected $name = 'command:name';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Command description.';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function fire()
{
$request = Request::create('api/notifications/send/unread', 'GET', array($this->getArguments()));
Route::dispatch($request)->getContent();
}
/**
* Get the console command arguments.
*
* #return array
*/
protected function getArguments()
{
return array(
array('frequency', InputArgument::OPTIONAL, 'How often should the email be sent', 'H'),
);
}
/**
* Get the console command options.
*
* #return array
*/
protected function getOptions()
{
return array(
array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
);
}
}
I cannot see why I would be getting the too many arguments exception?

You do only have one argument set, for frequency
protected function getArguments()
{
return array(
array('frequency', InputArgument::OPTIONAL, 'How often should the email be sent', 'H'),
);
}
so the
php artisan command:name sendUnreadNotifications H --env=local
here the H is the argument that is too much. You should change your command's name to what you want to do, the command names need to be unique btw...
Change this:
protected $name = 'command:name';
to
protected $name = 'send:unreadNotifications';
and run your job with
php artisan send:UnreadNotifications H
and it will work.

Related

Laravel Scheduler Cron Job

I've created a command named SendAutoEmail and i'm running the command by this
php artisan send:autoemail
command is working properly but it's not sending an email, i've added direct Email function into command and also tried to add email function into Controller method and called that controller into Command file but email is not sending but if i'm trying to call the same method via url its sending email successfully, i don't know what is the issue, here is the code
Commands>SendAutoEmail.php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Http\Controllers\PassportController;
class SendAutoEmail extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'send:autoemail';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return int
*/
public function handle()
{
$controller = new PassportController();//your controller name
$controller->sendEMail();
dd($controller);
\Log::info('Cron is working fine!');
$this->info('Send:AutoEmail cron Command RUn Seccessfully');
return 0;
}
}
Console>Kernel.php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Console\Commands\SendAutoEmail;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
SendAutoEmail::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('send:autoemail')
}
}
and the controller method from where i'm trying to send email.
public function sendEMail(){
$alerts = [
'passport_no' => 'ex12312312',
'name' => 'test name',
'expiry_date' => '01-11-2020',
'id_no_cnic' => '12312-3123123-1'
];
$emailSent = Mail::to('test.email#gmail.com')->send(new ExpireAlert($alerts));
DB::table('tbl_test')->insert(
['data_text' => 'test data new']
);
dd($emailSent);
// here on dd($emailSent) i'm getting 0 in response
DB::table('tbl_test')->insert(
['data_text' => 'test data']
);
print_r($alerts);
}
Have you run the scheduler?
php artisan schedule:run
Then you should see your mail in the queue table within your database.
Now, to execute the queue:
php artisan queue:work
In production, the package Supervisor is recommended. What it does is to make sure the worker always is running.
Source: https://laravel.com/docs/8.x/queues#running-the-queue-worker

ARGUMENTS and OPTIONS with Laravel Illuminate Console Command

It's pretty clear how to create a simple command for the CLI in Laravel and this goes also for creating arguments, but I can't seem to find a simple documentation on how to create options for the CLI.
I want to make it so that the user can add options after the argument.
So for example:
php artisan cmd argument -a
php artisan cmd argument -a -c -x
How do I implement such a structure in the class below?
Updated code
There are indeed a few possible solutions. It was actually quit easy.
class cmd extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'cmd {argument}
{--s : description}
{--x : description}';
/**
* The console command description.
*
* #var string
*/
protected $description = 'description';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$var = $this->argument('argument');
$options = $this->options();
new Main($var,$options);
}
}
There are a lot of posible solutions for this, but I prefer to add optional arguments and if they exist do determinate actions with the ? that means argument can exist or not, plus * this means can be more tan one, like this:
class cmd extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'cmd {argument} {-extra*?}';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$var = $this->argument('argument');
if($this->argument('-extra')) {
//do things if -extra argument exists, it will be an array with the extra arguments value...
}
new Main($var);
}
}
There a whole doc section dedicated to creating commands from scratch and it's well documented. Look through it.
https://laravel.com/docs/5.4/artisan
If you need to learn from examples then have a look here into all the laravel's built in console commands.
vendor/laravel/framework/src/Illuminate/Foundation/Console

Laravel5 console not registerring

As part of my Laravel 5.2 application, I would like to define a custom command for artisan, but my command doesn't appear in artisan list.
1). I created the command skeleton: artisan make:console --command=process:emails
2). I added a bit of test code to the handle() method of the new class:
<?php
namespace App\Console\Commands;
use App\CommunicationsQueue;
use Illuminate\Console\Command;
class ProcessEmailQueueCommand extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'process:email';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Send all currently pending emails in the queue';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
CommunicationsQueue::where('status', 'PENDING')->update(['status'=>'TEST']);
$this->info('The mails queue was successfully processed.');
}
}
3). Then, I registerred the command in app/Console/Kernel.php:
protected $commands = [
'App\Console\Commands\ProcessEmailQueueCommand',
];
What am I missing here? I'm sure it's something incredibly simple, but I'm not seeing it.
in app/Console/Kernel.php try with the following code
protected $commands = [
'App\Console\Commands\ProcessEmailQueueCommand',
];
On Kernel.php, try include the full name, which should be:
protected $commands = [
\App\Console\Commands\ProcessEmailQueueCommand::class,
];

Set Up CronJob In CPanel Laravel

i set up a cronjob in cpanel, i'm using laravel 4.2. i also set to send the cronjob to my email and below yellow part is the error i received in email.
[InvalidArgumentException]
There are no commands defined in the "user" namespace.
//laravel command.php
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class CronDelFilesCommand extends Command {
/**
* The console command name.
*
* #var string
*/
protected $name = 'user:active';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Command description.';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function fire()
{
echo "aaa";
}
/**
* Get the console command arguments.
*
* #return array
*/
protected function getArguments()
{
return array(
);
}
/**
* Get the console command options.
*
* #return array
*/
protected function getOptions()
{
return array(
array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
);
}
}
//command in cpanel
* * * * * /usr/bin/php /home/project/public_html/artisan user:active > /home/project/public_html/log.txt
what's wrong? what's the error mean?
UPDATED
thanks to #KristianHareland i use wget. :)
I think you forget to add this command to artisan app/start/artisan.php.
You shoud wright:
Artisan::add(new CronDelFilesCommand());
And don't forget about composer dump-autoload
More info you can find here.

PHP Artisan Custom Command

I need a custom command in my php artisan.
I have created the command file via artisan shell.
But when I execute it via php artisan it throws it as undefined.
It is also confusing for me how to add arguments to my command.
Here is my whole command file:
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class createLibrarySet extends Command
{
/**
* The console command name.
*
* #var string
*/
protected $name = 'createLibrarySet';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Creates a Library Set.';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function fire()
{
$dir = mkdir(app_path()."/".library."/".$this->library_name);
$coreName = $this->library_name;
$serivceProvider = $coreName."."."ServiceProvider.php";
$library = $coreName."."."Library.php";
$facade = $coreName."."."Facade.php";
$interface = $coreName."."."Interface.php";
fopen($dir."/".$library, "w");
fopen($dir."/".$facade, "w");
fopen($dir."/".$serivceProvider, "w");
fopen($dir."/".$interface, "w");
}
/**
* Get the console command arguments.
*
* #return array
*/
protected function getArguments()
{
return array(
array('example', InputArgument::REQUIRED, 'An example argument.'),
);
}
/**
* Get the console command options.
*
* #return array
*/
protected function getOptions()
{
return array(
array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
);
}
}
You have to add the command to your app/start/artisan.php:
Artisan::resolve('createLibrarySet');
Now you should be able to see in the
php artisan
listing.
To get your arguments, you just have to:
$email = $this->argument('email');
And options
$force = $this->option('force');

Categories