PHP code example of patienceman / synca

1. Go to this page and download the library: Download patienceman/synca 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/ */

    

patienceman / synca example snippets


> QUEUE_CONNECTION=database 
> 

App\Notifiers

namespace App\Notifications;

use Patienceman\Synca\NotifyHandler;

class EmailNotification extends NotifyHandler {
    /**
     * Execute notification actions
     * 
     * @return mixed
     */
    public function handle() {
        // do whatever action inside handler
    }
}

namespace App\Http\Controllers;

use App\Notifications\EmailNotification;
use Patienceman\Synca\Facades\Notifier;

class UsersController extends Controller {

    /**
     * Handle User Notifications
     */
    public function notifications(Notifier $notifier) {
        // ... Other Codes

        $notifier->handle([
            EmailNotification::process([ 
                'message' => 'Application sent to job sent' 
            ]),
        ]);
    }

}

namespace App\Http\Controllers;

use App\Notifications\EmailNotification;
use Patienceman\Synca\Facades\Notifier;

class UsersController extends Controller {
    /**
     * Handle User Notifications
     */
    public function notifications(Notifier $notifier, User $user) {
        // ... Other Codes

        $application = Application::findById('1')->belongsToCompany()->user_id;
        $notification = [ 'message' => 'Application sent to job sent' ];
        
        $users = [
            'user' => $user
            'applicant' => $application
        ];

        $notifier->handle([
            EmailNotification::process($notification)
                ->to($users)
                ->onQueue(),
        ]);
    }

}

$this->user;
$this->applicant;

namespace App\Http\Controllers;

use App\Notifications\EmailNotification;
use App\Notifications\OneSignalNotification;
use Patienceman\Synca\Facades\Notifier;

class UsersController extends Controller {

    public function notifications(Notifier $notifier, User $user) {
        $application = Application::findById('1')->belongsToCompany()->user_id;
        $notification = [ 'message' => 'Application sent to job sent' ];
        
        $users = [
            'user' => $user
            'applicant' => $application
        ];

        $notifier->handle([
            EmailNotification::process($notification)->to($users),
            OneSignalNotification::process($notification)->to([$user]),
        ])->onQueue();
    }

}

namespace App\Notifications;

use Patienceman\Synca\NotifyHandler;

class EmailNotification extends NotifyHandler {
    /**
     * Execute notification actions
     * @return mixed
     */
    public function handle() {
        $this->message; // this will get single passed payload
        $this->payload(); // this will get all payload as object
        $this->recipients(); // this will get all targeted users
    }
}

$this->foreachUser(fn($user) => $this->sendToDatabase($user)); 

namespace App\Notifications;

use Patienceman\Synca\NotifyHandler;

class DatabaseNotification extends NotifyHandler {
    /**
     * Execute notification
     * @return mixed
     */
    public function handle() {
        $this->foreachUser(
            fn($user) => $this->sendToDatabase($user, $this)
        );
    }

    /**
     * Get the array to database representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toDatabase($notifiable) {
        return $this->payloadAsArray();
    }
}

namespace App\Notifications;

use Patienceman\Synca\NotifyHandler;

class DatabaseNotification extends NotifyHandler {
    /**
     * Execute notification
     * @return mixed
     */
    public function handle() {
        $this->foreachUser(function($user) {
            $this->dbNotification($user, fn ($notifiable) => [
                'header' => $this->subject,
                'message' => $this->message,
                'action' => $this->action,
            ]);
        });
    }
}
bash  
> php artisan queue:table
> php artisan notification:table
> php artisan migrate
> 
bash
php artisan make:notifier EmailNotification
bash
php artisan make:notifier Model/EmailNotification
bash
$this->foreachUser()