Laravel

Laravel use notification when some data add

Laravel use notification when some data add

Laravel use notification when some data add

今天就舉一個例子,如果有商家新增了,將通知給使用者

1. 下載 laravel notofocation 資料表

php artisan notifications:table

php artisan migrate

2. 創建 notification

php artisan make:notification StoreAdded

3. 撰寫 StoreAdded.php 回傳程式碼

toarray 函式,就是我們要發送的資料,會是一個 json。 因為是使用 database,所以要將 via 裡改成 database

<?php

namespace App\Notifications;

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

class StoreAdded extends Notification
{
    use Queueable;

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

    /**
     * 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 (new MailMessage)
            ->line('The introduction to the notification.')
            ->action('Notification Action', url('/'))
            ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param mixed $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            'message' => $this->store->name . '加入囉!',
            'date' => Carbon::now()->format('m-d H:i'),
        ];
    }
}

4. 在 User 模型加入 Notifiable

<?php

namespace App;

use App\Notifications\StoreAdded;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    public function sendStoreAddedNotification($store)
    {
        $this->notify(new StoreAdded($store));
    }
}

5. 新增商家發送通知

將發送通知程式碼接在 save 後面,當新增成功才將通知加到資料庫,user 可以設定要傳給哪些人

// 找到所有使用者發送
$users = User::all();

foreach ($users as $user) {
    $user->sendStoreAddedNotification($store);
}

6. 讀取通知

以下為讀取所有通知

$user=App\Models\User::find(1);

foreach ($user->notifications as $notification) {
    echo $notification->type;
}

以下為讀取未讀取通知

$user=App\Models\User::find(1);

foreach ($user->unreadNotifications as $notification) {
    echo $notification->type;
}

7. 將通知變成已讀取

可以一條一條設為已讀取

$user=App\Models\User::find(1);

foreach ($user->unreadNotifications as $notification) {
    $notification->markAsRead();
}

也可以一次把撈出來的 user 通知設為已讀取 $user->unreadNotifications->markAsRead();

取出單筆通知(不一定是個好方法)

$user = App\Models\User::find(1);
$notification = $user->notifications()->find($notificationid);
if($notification) {
    $notification->markAsRead();
}

刪除通知 $user->notifications()->delete();

補充

內建 notification 模型放在這裡,可以使用普通數據庫查詢 Illuminate\Notifications\DatabaseNotification

comments powered by Disqus