Skip to content

Instantly share code, notes, and snippets.

@pbelyaev
Last active September 5, 2017 15:11
Show Gist options
  • Save pbelyaev/543a3952883fb1158c07c08029212d28 to your computer and use it in GitHub Desktop.
Save pbelyaev/543a3952883fb1158c07c08029212d28 to your computer and use it in GitHub Desktop.
Eloquent notifiable model that can send notifications to Slack (without any attachment to a model)
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Notifications\Notification;
class SlackNotification extends Model
{
use Notifiable;
/**
* @var string
*/
private $slack_webhook_url;
/**
* @param Notification $notification
* @param string $slackWebhookUrl
* @return SlackNotification
*/
public static function sendNotification(Notification $notification, string $slackWebhookUrl): SlackNotification
{
return tap(
(new static),
function (SlackNotification $slackNotification) use ($slackWebhookUrl, $notification) {
$slackNotification->slack_webhook_url = $slackWebhookUrl;
$slackNotification->notify($notification);
}
);
}
/**
* @return string
*/
public function routeNotificationForSlack(): string
{
return $this->slack_webhook_url;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment