How can I create an artisan command to send a database notification to all users in the system containing information of how long they have been in the system for?
My SendEmails Command looks like the following:
<?php namespace AppConsoleCommands; use IlluminateConsoleCommand; use AppUser; use IlluminateSupportFacadesMail; use AppMailUserEmails; use IlluminateSupportFacadesNotification; class SendEmails extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'send:emails'; /** * The console command description. * * @var string */ protected $description = 'Send Email to allusers'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { $users = User::all(); foreach($users as $user){ $created_at = $user->created_at; Notification::send($user, new SendEmailsNotification($created_at)); } } }
Then I created the notifications table, migrated and the code is the below:
<?php namespace AppNotifications; use IlluminateBusQueueable; use IlluminateNotificationsNotification; use IlluminateContractsQueueShouldQueue; use IlluminateNotificationsMessagesMailMessage; class SendEmailsNotification extends Notification { use Queueable; public $created_at; public function __construct($created_at) { $this->created_at = $created_at; } public function via($notifiable) { return ['database']; } public function toMail($notifiable) { return (new MailMessage) ->line('The introduction to the notification.') ->action('Notification Action', url('/')) ->line('Thank you for using our application!'); } public function toArray($notifiable) { return [ ]; } }
User.php:
<?php namespace App; use IlluminateNotificationsNotifiable; //use IlluminateContractsAuthMustVerifyEmail; use IlluminateFoundationAuthUser as Authenticatable; class User extends Authenticatable { //use Notifiable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', 'address', 'image' ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ //'email_verified_at' => 'datetime', 'address' => 'array' ]; protected $uploads = '/images/'; public function getImageAttribute($image){ return $this->uploads . $image; } public function contacts(){ return $this->hasMany('AppContact'); } }
When I run the artisan command “php artisan send:emails” I see the following error in the console:
BadMethodCallException : Call to undefined method AppUser::routeNotificationFor()
How can I send the notification to all users?
Advertisement
Answer
You just need to uncomment the // use Notifiable
line.
The Notifiable
trait includes two other traits, one of which is the RoutesNotifications
trait.
The RoutesNotifications
trait is what you need to be able to send Notifications to a User
.
Also, you should be able to simplify your code in your SendEmails
command to be:
Notification::send(User::all(), new SendEmailsNotification());
And instead of explicitly passing created_at
you can just access it from $notifiable
in your SendEmailsNotification
(as in this case $notifiable
will be the User
model anyway) e.g.
public function toArray($notifiable) { return [ 'data' => 'Account Created' . $notifiable->created_at->diffForHumans() ]; }
}