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\BroadcastMessage;
use Codewiser\Notifications\Messages\DatabaseMessage;
use Codewiser\Notifications\MarkdownTable;
class ReviewArticle extends \Illuminate\Notifications\Notification
{
protected function build(MessageContract $message)
{
$message
->subject('Article Review')
->line('You need to review the article.')
->action('Review', url('/article', [
'article' => $this->article->getKey()
]))
// Format as blockquote
->quotation('Silence is golden');
}
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'])
->render()
);
}
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\Builders\NotificationBuilder;
use Codewiser\Notifications\Models\DatabaseNotification;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;
class User extends Model
{
public function notifications(): MorphMany|NotificationBuilder
{
return $this->morphMany(DatabaseNotification::class, 'notifiable');
}
}
use Codewiser\Notifications\Messages\DatabaseMessage;
class PostCommentNotification extends \Illuminate\Notifications\Notification
{
public function __construct(public Comment $comment) {
//
}
public function toDatabase($notifiable): DatabaseMessage
{
return (new DatabaseMessage)
->subject('New comment')
->attach($this->comment)
->attach($this->comment->post);
}
}
// User's unread notifications attached to any post:
$request->user()->notifications()
->whereMentioned(\App\Models\Post::class)
->whereUnread()
->count();
// User's unread notifications attached to the exact post and to any comments:
$request->user()->notifications()
->whereMentioned([
$post,
\App\Models\Comment::class
])
->whereUnread()
->count();
return (new DatabaseMessage)
->subject('New comment')
->attach($this->comment)
->attach($this->comment->post, ['relevant' => true]);
// User's unread notifications attached to any post that is flagged as relevant:
$request->user()->notifications()
->whereMentioned([
\App\Models\Post::class => fn($query) => $query
->where('notification_mention.relevant', true),
])
->whereUnread()
->count();
use Codewiser\Notifications\Messages\DatabaseMessage;
use Codewiser\Notifications\Models\DatabaseNotification;
use Codewiser\Notifications\Builders\NotificationBuilder;
// Send a persistent notification with the article attached.
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')
->attach($this->article);
}
}
// Get unread notifications about an article
$article->mentions()
->where(fn (NotificationBuilder $builder) => $builder
->whereNotifiable($user)
->whereUnread()
);
// Later... mark the notification as read if the article was reviewed.
if ($article->wasReviewed()) {
$user->notifications()
->whereType(ReviewArticleNotification::class)
->whereMentioned($article)
->markAsRead();
}
use Codewiser\Notifications\Contracts\Mentionable;
use Codewiser\Notifications\Traits\HasMentions;
use Illuminate\Database\Eloquent\Model;
class Article extends Model implements Mentionable
{
// Provides mentions relation
use HasMentions;
}