Store and fetch the notifications from database using Laravel Notifications

Category:
Laravel
Tags:
Laravel

Sreerag Nair
6 months ago

1. Generate notification using the notification table

php artisan notifications:table

2. Run the migrations

php artisan migrate

3. Define the data to be displayed in toArray method of your notification class

public function toArray(object $notifiable): array{    
           return [        
                 'message' => 'Jhon doe has paid the amount',    
          ];
}

4. Save the notification in the database by specifying the 'database' in via method of your notification class.

public function via(object $notifiable): array    {        
        return ['mail','database'];   
 }

5. Read all the user notifications

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


6. Delete the notification

$user->notifications()->delete();


7. Read the notification

$user->unreadNotifications->markAsRead();
DevZone
Made by developer, Made for developers