twitter
twitter copied to clipboard
Set Twitter username in TwitterDirectMessage from User model routeNotificationForTwitter
I have a routeNotificationForTwitter
on my User
model which returns the Twitter username for my user, how can I access this within my notification's toTwitter
method?
/**
* Route the notification for Twitter.
*
* @return string
*/
public function routeNotificationForTwitter($notification)
{
try {
$availableIntegration = AvailableIntegration::where('slug', 'twitter')
->first();
$userIntegration = UserIntegration::where('user_id', $this->id)
->where('available_integration_id', $availableIntegration->id)
->first();
if (!$userIntegration) {
return null;
}
return $userIntegration->schema->twitter_username;
} catch (\Exception $e) { }
return null;
}
/**
* Get the Twitter representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toTwitter($notifiable)
{
$data = [
'type' => 'monitor-down',
'name' => $this->emailData['monitor_name'],
'url' => $this->emailData['monitor_url'],
'value' => 'down'
];
try {
$this->createHistoryEntry('twitter', 'Monitor Down', $data, $notifiable);
} catch (\Exception $e) { }
$monitorName = $this->emailData['monitor_name'];
$monitorURL = $this->emailData['monitor_url'];
return new TwitterDirectMessage("factfinder_all", "Your monitor $monitorName -- ($monitorURL) -- has just gone DOWN");
}
You can send the $user to the notification class as a parameter of the __construct() method. In inside, you can save it like $this->user = $user. Then you can use it toTwitter() method.
That was the answer to "how to reach $user in the notification class" above. But with the routeNotificationForTwitter() method you don't need anything. The method does it by itself.