Laravel Notification to Database

Contoh class Notification buat ngasih notif khusus ke database aja. Ini contoh kalau dipanggil saat ada pelanggan yang baru bayar order.

Di listener manggilnya: Notification::send(access()->getAllAdmins(), new \App\Notifications\Frontend\Order\HasPaid($order));

<?php

namespace App\Notifications\Frontend\Order;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

use App\Models\Access\User\UserOrder;

class HasPaid extends Notification
{
    use Queueable;

    protected $order;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct(UserOrder $order)
    {
        $this->order = $order;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['database'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return [
            // 
        ];
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }

    /**
     * Get the database representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return database
     */
    public function toDatabase($notifiable)
    {
        return [
            'name' => $this->order->user->first_name.' '.$this->order->user->last_name,
            'description' => 'melakukan konfirmasi pembayaran',
            'url' => route('admin.payment.order.show', $this->order)
        ];
    }
}


About Author

R. Muhamad Dwi Rizki Lazuardi

Toprak DG


Comment & Discussions

    Please LOGIN before if you want to give the comment.