Laravel Notifications ile bir işlemden sonra bildirim gönderebilirsiniz. Bildirim yöntemleri arasında mail, sms ve Slack bulunmaktadır. Örnek kullanımına fatura kesildikten sonra faturayı mail ve sms ile üyeye gönderme verilebilir.
Laravel Notifications Oluşturma
Oluşturulacak dosya ‘app/Notifications’ dizininde oluşur.
php artisan make:notification OdemeFaturaNotification php artisan make:notification OdemeFaturaNotification --markdown=mail.fatura.odeme // markdown şablonlu mail component'ı oluşturur.
Eğer Notification’ı veritabanına kaydedecekseniz;
php artisan notifications:table php artisan migrate
Laravel Notifications Gönderme
Oluştuduğunuz dosyayı açın. Eğer veritabanına eklemek isterseniz toDatabase metodu da kullanmanız gerekiyor.
// app/Notifications/MyFirstNotification.php namespace App\Notifications; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Notification; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; class OdemeFaturaNotification extends Notification { use Queueable; private $details; public function __construct($details) { $this->details = $details; } public function via($notifiable) { return ['mail','database']; } public function toMail($notifiable) { return (new MailMessage) ->subject('Notification Subject') ->greeting($this->details['greeting']) ->line($this->details['body']) ->action($this->details['actionText'], $this->details['actionURL']) ->line($this->details['thanks']) ->attach('/path/to/file'); } public function toDatabase($notifiable) { return [ 'order_id' => $this->details['order_id'] ]; } }
Notifiable trait veya Notification facade kullanılarak işlem gerçekleştirilebilir. Notifications gönderilecek Model dosyasına Notifiable trait’i eklenmesi gerekir. Örnek Kullanım:
<?php namespace App\Models; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends Authenticatable { use Notifiable; }
-İşlemi gerçekleştirmek istediğiniz alanda aşağıdaki gibi notification’ı çalıştırabilirsiniz. Bu yöntemde notify metodu kullanılmıştır.
use App\Notifications\OdemeFaturaNotification; $user->notify(new OdemeFaturaNotification($invoice));
-Notification Facade kullanımı ise şu şekildedir;
use Illuminate\Support\Facades\Notification; Notification::send($users, new OdemeFaturaNotification($invoice));
İlk Yorumu Siz Yapın