PHP code example of codewiser / laravel-notifications
1. Go to this page and download the library: Download codewiser/laravel-notifications library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
codewiser / laravel-notifications example snippets
use Codewiser\Notifications\Contracts\MessageContract;
use Codewiser\Notifications\Messages\MailMessage;
use Codewiser\Notifications\Messages\TelegramMessage;
use Codewiser\Notifications\Messages\BroadcastMessage;
use Codewiser\Notifications\Messages\DatabaseMessage;
class ReviewArticle extends \Illuminate\Notifications\Notification
{
protected function build(MessageContract $message)
{
$message
->subject('Article Review')
->line('You need to review article.')
->action('Review', url('/article', [
'article' => $this->article->getKey()
]))
// Format as blockquote
->quotation('Silent is gold');
}
public function toMail(): MailMessage
{
return (new MailMessage)
->tap(fn($message) => $this->build($message))
// Markdown table
->table(fn(MarkdownTable $table) => $table
->row(['Title 1', 'Title 2'])
->row([':---', '---:'])
->row(['Text 1', 'Text 2'])
);
}
public function toTelegram(): TelegramMessage
{
return (new TelegramMessage)
->tap(fn($message) => $this->build($message))
// Do not render preview
->linkPreviewOptions(is_disabled: true)
// Prevent message forwarding or saving
->protected()
// Notification without a sound
->silently();
}
public function toBroadcast(): BroadcastMessage
{
return (new BroadcastMessage)
->tap(fn($message) => $this->build($message))
// Remove action button
->withoutAction()
// Keep notification on screen until user closes it
->
use Codewiser\Notifications\Messages\DatabaseMessage;
use Codewiser\Notifications\Models\DatabaseNotification;
use Codewiser\Notifications\Builders\NotificationBuilder;
// Notification
class ReviewArticleNotification extends \Illuminate\Notifications\Notification
{
public function toDatabase(): DatabaseMessage
{
return (new DatabaseMessage)
->subject('Review article')
->action('Review', route('article.show', $this->article))
->persistent('You must review the article')
->bindTo($this->article);
}
}
// Get unread notifications about an article
$article->mentions()
->where(fn (NotificationBuilder $builder) => $builder
->whereNotifiable($user)
->whereUnread());
// Later...
if ($article->wasReviewed()) {
$user->notifications()
->whereType(ReviewArticleNotification::class)
->whereMentioned($article)
->whereUnread()
->markAsRead();
}
use \Codewiser\Notifications\Contracts\Mentioned;
use \Codewiser\Notifications\Traits\HasMentions;
use \Illuminate\Database\Eloquent\Model;
class Article extends Model implements Mentioned
{
// Provides mentions relation
use HasMentions;
}