Last active
April 15, 2019 17:56
-
-
Save victorsteven/2684043b09c710eb54b7a2a89c92abad to your computer and use it in GitHub Desktop.
NotifyUsers Artisan Command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
use App\Message; | |
use Carbon\Carbon; | |
use App\Jobs\SendMailJob; | |
use App\User; | |
use App\Mail\NewArrivals; | |
class NotifyUsers extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'notify:users'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Send an email to users'; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() | |
{ | |
//One hour is added to compensate for PHP being one hour faster | |
$now = date("Y-m-d H:i", strtotime(Carbon::now()->addHour())); | |
logger($now); | |
$messages = Message::get(); | |
if($messages !== null){ | |
//Get all messages that their dispatch date is due | |
$messages->where('date_string', $now)->each(function($message) { | |
if($message->delivered == 'NO') | |
{ | |
$users = User::all(); | |
foreach($users as $user) { | |
dispatch(new SendMailJob( | |
$user->email, | |
new NewArrivals($user, $message)) | |
); | |
} | |
$message->delivered = 'YES'; | |
$message->save(); | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment