PHP code example of toneflix-code / approvable-notifications

1. Go to this page and download the library: Download toneflix-code/approvable-notifications library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

toneflix-code / approvable-notifications example snippets


ToneflixCode\ApprovableNotifications\ApprovableNotificationsServiceProvider::class

'ApprovableNotifications' => ToneflixCode\ApprovableNotifications\Facades\ApprovableNotifications::class

namespace App\Models;

use ToneflixCode\ApprovableNotifications\Traits\SendsApprovableNotifications;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use SendsApprovableNotifications;
}

namespace App\Models;

use ToneflixCode\ApprovableNotifications\Traits\HasApprovableNotifications;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasApprovableNotifications;
}

namespace App\Models;

use ToneflixCode\ApprovableNotifications\Traits\ApprovableNotifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use ApprovableNotifiable;
}

$faker = \Faker\Factory::create();
$user = \App\Models\User::find(1);
$sender =\App\Models\User::find(12);
$actionable =\App\Models\User::find(5);

$notif = $sender->sendApprovableNotification(
    recipient: $user, // The recieving model
    title: $faker->sentence(4), // The title of the notification
    message: $faker->sentence(10), // The notification text message body
    data: ['icon' => 'fas fa-info'], // Any extra data you would like to store,
    actionable: $actionable, // Any model you would like to access or reference later during retrieval
);

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

foreach ($user->approvableNotifications as $notification) {
    echo $notification->title;
}

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

foreach ($user->unreadApprovableNotifications as $notification) {
    echo $notification->title;
}

foreach ($user->approvedApprovableNotifications as $notification) {
    echo $notification->title;
}

foreach ($user->approvableNotifications as $notification) {
    $sender = $notification->notifier;
    echo $sender->name;
}

foreach ($user->approvableNotifications as $notification) {
    $actionable = $notification->actionable;
    echo $actionable->title;
}

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

foreach ($user->approvableSentNotifications as $notification) {
    echo $notification->title;
}

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

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

$user->unreadApprovableNotifications->markAsRead();

$user = App\Models\User::find(1);
$user->unreadApprovableNotifications()->update(['read_at' => now()]);

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

foreach ($user->approvableNotifications as $notification) {
    echo $notification->markAsApproved();
}

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

foreach ($user->approvableNotifications as $notification) {
    echo $notification->markAsRejected();
}

$user->approvableNotifications->markAsApproved();

$user->approvableNotifications->markAsRejected();

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

namespace App\Models;

use ToneflixCode\ApprovableNotifications\Traits\SendsApprovableNotifications;
use Illuminate\Foundation\Auth\User as Authenticatable;
use ToneflixCode\ApprovableNotifications\Models\Notification;

class User extends Authenticatable
{
    use SendsApprovableNotifications;

    public function newNotificationCallback(Notification $notification) {
        // Perform any other actions here when a notification is created
    }
}

namespace App\Models;

use ToneflixCode\ApprovableNotifications\Traits\HasApprovableNotifications;
use Illuminate\Foundation\Auth\User as Authenticatable;
use ToneflixCode\ApprovableNotifications\Models\Notification;

class User extends Authenticatable
{
    use HasApprovableNotifications;

    public function approvedNotificationCallback(Notification $notification) {
        // Perform any other actions here when a notification is approved
    }

    public function rejectedNotificationCallback(Notification $notification) {
        // Perform any other actions here when a notification is rejected
    }
}
shell
   php artisan vendor:publish --tag=approvable-notifications
   
shell
   php artisan migrate