Laravel Update Notification – Easily send fast updates through Slack and Many More
Laravel Development Company, new updates will allow to send every updates through the Slack, SMS, Email and integrated with Many More.
Lets take look, how easy it is to build and send a notification using this new feature: class NewPost extends \Illuminate\Notifications\Notification
{
public function __construct($post)
{
$this->post = $post;
}
public function via($notifiable)
{
return ['database'];
}
public function toArray($notifiable)
{
return [
'message' => 'A new post was published on Laravel News.',
'action' => url($this->post->slug)
];
}
}
All you need to do now is to send the notification to the selected users: $user->notify(new NewPost($post));
Creating Notification for Sending SMS :
Laravel integrate with a new console command for creating notifications: php artisan make:notification NewPost
This will create a new class in app/Notifications, each notification class contains a via() method as well as different methods for building notifications for different channels.
Using the via() method you can specifying the channels you’d like this particular notification to be sent through, check this example for the official documentation website:
public function via($notifiable)
{
return $notifiable->prefers_sms ? ['sms'] : ['mail', 'database'];
}
The via method receives a $notifiable instance, which is the model you’re sending the notification to, in most cases it’ll be the user model but it’s not limited to that.
Channels we can used : Mail, Nexmo, Database, Broadcast and slack.
We format the Email Notification Easily :
We can customize the Email Notification format for instance, let’s take a look at formatting a mail notification: public function toMail($notifiable)
{
return (new MailMessage)
->subject('New Post!')
->line('A new post was published on Laravel News!')
->action('Read Post', url($this->post->slug))
->line('Please don\'t forget to share.');
}
This will create a mail message using a nice built-in responsive template that you can publish using the vendor:publish console command.
The notification system will automatically look for an email property in your model, however, you can customise this behaviour by defining a routeNotificationForMail in your Model and return the email address this Model will be contacted on.
Sending Notifications
You can send notifications using the notify() method on your model, this method exists on the Notifiable trait which you’ll need to add to your Model. $user->notify(new NewPost($post));
You can also use the Notification facade, this will allow you to send notifications to multiple notifiables at the same time: Notification::send(User::all(), new NewPost($post));
Queueing Notifications :
You can easily queue sending notifications by using the ShouldQueue interface on the Notification class and including the Queueable trait. <?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
class NewPost extends Notification implements ShouldQueue
{
use Queueable;
}
Contact us : Contact@expresstechsoftwares.com
Post a Comment