1
I created a cron for sending email every period of time. But how to test the operation ?
Code
<?php
namespace App\Console\Commands;
namespace App\Console\Commands\EnvioEmailBlCron;
use Illuminate\Console\Command;
class envioEmailBIcron extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'envioEmailBI:cron';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command Email';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//Pegar os dados no banco
$sql = ' select * from payments as P, receipts as R ';
$sql .= ' where P.created_at < CURRENT_DATE AND P.created_at < CURRENT_DATE -1';
$sql .= ' OR R.created_at < CURRENT_DATE AND R.created_at < CURRENT_DATE -1';
//pega os dados no banco
$dados = \DB::select($sql);
//envio email
Mail::send('emails.BI', $dados, function ($message) {
$message->to(Input::get('email'));
});
// executando as funções de envio de e-mail
$this->info('Example Cron comando rodando com êxito');
}
}
Consolekernel.php
<?php
namespace App\Units;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel;
class ConsoleKernel extends Kernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//Commands\Inspire::class,
Commands\EnvioEmailBICron::class
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
*/
protected function schedule(Schedule $schedule)
{
//configuração do cron
$schedule->command('inspire')->dailyAt('01:00');
$schedule->command('EnvioEmailBICron:cron')->daily(); // email diários
}
/**
* Register the Closure based commands for the application.
*/
protected function commands()
{
}
}
I am receiving the message: Class App Units Commands Inspire does not exist
– alexjosesilva
after removing a Commands Inspire::class line from C
– alexjosesilva
consoleKernel received this message
– alexjosesilva
[Reflectionexception] Class App Units Commands enviedEmailBIcron does not exist
– alexjosesilva
You have to put the namespace correctly and check the paths, take a look this is your local error. @alexjosesilva
– novic
the class name is this: mailEmailBIcron
– alexjosesilva
The class name is already outside the Laravel standard (Laravel follows some nomenclatures is pretty cool because it puts the code on an equality and ease of maintenance) the correct name should be
EnvioEmailBlCron
she has a namespace you added this with the commanduse
? @alexjosesilva– novic
Just remembering that the above answer works and has been tested... with Laravel 5.3 of the official package. @alexjosesilva
– novic
use was not used!
– alexjosesilva
I have this statement Envioemailbicron.php : namespace App Console Commands; use Illuminate Console Command;
– alexjosesilva
Okay, but where are you playing this class needs to have her namespace, ie,
App\Console\Commands\EnvioEmailBlCron
I think that’s what’s missing. @alexjosesilva– novic
in the file Envioemailbicron.php: namespace App Console Commands Envioemailblcron;
– alexjosesilva
but the error persists!
– alexjosesilva
Edit your question and put all the classes and how you’re calling! I can’t catch the bug. @alexjosesilva
– novic
added namespace and in Envioemailbicron.php
– alexjosesilva
I added the.php kernel script.
– alexjosesilva
Let’s go continue this discussion in chat.
– novic