--- tags: PHP, Laravel, Backend --- # Laravel use notification when some data add #### 今天就舉一個例子,如果有商家新增了,將通知給使用者 ### 1. 下載laravel notofocation資料表 ```shell= php artisan notifications:table php artisan migrate ``` ### 2. 創建notification ```shell= php artisan make:notification StoreAdded ``` ### 3. 撰寫StoreAdded.php回傳程式碼 toarray函式,就是我們要發送的資料,會是一個json。 因為是使用database,所以要將via裡改成database ```php= <?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 <?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可以設定要傳給哪些人 ```php= // 找到所有使用者發送 $users = User::all(); foreach ($users as $user) { $user->sendStoreAddedNotification($store); } ``` ### 6. 讀取通知 以下為讀取所有通知 ```php $user=App\Models\User::find(1); foreach ($user->notifications as $notification) { echo $notification->type; } ``` 以下為讀取未讀取通知 ```php $user=App\Models\User::find(1); foreach ($user->unreadNotifications as $notification) { echo $notification->type; } ``` ### 7. 將通知變成已讀取 可以一條一條設為已讀取 ```php $user=App\Models\User::find(1); foreach ($user->unreadNotifications as $notification) { $notification->markAsRead(); } ``` 也可以一次把撈出來的user通知設為已讀取 `$user->unreadNotifications->markAsRead();` 取出單筆通知(不一定是個好方法) ```php= $user = App\Models\User::find(1); $notification = $user->notifications()->find($notificationid); if($notification) { $notification->markAsRead(); } ``` 刪除通知 `$user->notifications()->delete();` ### 補充 內建notification 模型放在這裡,可以使用普通數據庫查詢 `Illuminate\Notifications\DatabaseNotification`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up