PHP code example of iracode-com / filament-notification

1. Go to this page and download the library: Download iracode-com/filament-notification 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/ */

    

iracode-com / filament-notification example snippets




return [
    App\Providers\AppServiceProvider::class,
    App\Providers\Filament\AdminPanelProvider::class,
    \IracodeCom\FilamentNotification\FilamentNotificationServiceProvider::class, // <---
];

'providers' => ServiceProvider::defaultProviders()->merge( [

    \IracodeCom\FilamentNotification\FilamentNotificationServiceProvider::class, // <---

] )->toArray(),



namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use HasFactory, Notifiable; // <-- Notifiable is important

    protected $fillable = [
    
        // ...
        'prefers_bale',
        'prefers_telegram',
        'prefers_sms',
        'telegram_chat_id',
        'bale_chat_id',
        'phone'
        // ...
        
    ];
    
    public function routeNotificationForSms()
    {
        return $this->phone;
    }

    public function routeNotificationForBale()
    {
        return $this->bale_chat_id;
    }
    
    public function routeNotificationForTelegram()
    {
        return $this->telegram_chat_id;
    }
    
}



namespace App\Providers\Filament;

use IracodeCom\FilamentNotification\FilamentNotificationPlugin;
use Filament\PanelProvider;
use Filament\Panel;

class AdminPanelProvider extends PanelProvider
{
    public function panel( Panel $panel ) : Panel
    {
        return $panel
            ->plugins( [

                FilamentNotificationPlugin::make(), // <---
                
                FilamentNotificationPlugin::make()
                                            ->setGridColumnConfig() // optional
                                            ->setGridColumnNotify() // optional
                                            ->setSectionColumnSpan() // optional
                                            ->usingPage() // optional

            ] )
        ;
    }
}



namespace IracodeCom\FilamentNotification\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use IracodeCom\FilamentNotification\Notifications\Channels\BaleChannel;
use IracodeCom\FilamentNotification\Notifications\Channels\FilamentChannel;
use IracodeCom\FilamentNotification\Notifications\Channels\FarazSmsPatternChannel;
use IracodeCom\FilamentNotification\Notifications\Channels\TelegramChannel;


class UserNotification extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     */
    public function __construct( protected string $message, protected bool $bale = true, protected bool $telegram = true, protected bool $sms = true )
    {
    }

    /**
     * Get the notification's delivery channels.
     *
     * @return array<int, string>
     */
    public function via( $notifiable ) : array
    {
        $channels = [ FilamentChannel::class ];

        if ( $notifiable->prefers_telegram && $this->telegram )
        {
            $channels[] = TelegramChannel::class;
        }

        if ( $notifiable->prefers_sms && $this->sms )
        {
            $channels[] = FarazSmsPatternChannel::class;
        }

        if ( $notifiable->prefers_bale && $this->bale )
        {
            $channels[] = BaleChannel::class;
        }

        return $channels;
    }


    public function toTelegram( $notifiable ) : array
    {
        return [
            'text' => $this->message,
        ];
    }

    public function toSms( $notifiable ) : array
    {
        return [
            'body' => $this->message,
        ];
    }

    public function toBale( $notifiable ) : array
    {
        return [
            'text' => $this->message,
        ];
    }

    public function toFilament( $notifiable ) : array
    {
        return [
            'body' => $this->message,
        ];
    }

}

use IracodeCom\FilamentNotification\Notifications\UserNotification;

$user = \App\Models\User::find( 1 );
$user->notify(
    new UserNotification( 'Hello' )
);

use IracodeCom\FilamentNotification\Notifications\UserNotification;
use \App\Models\User;

foreach ( User::all() as $user )
{

    $user->notify( new UserNotification( 'Welcome' ) );

}

// Or

User::get()->each->notify( new UserNotification( 'Welcome' ) );

bash
php artisan vendor:publish --provider=IracodeCom\FilamentNotification\FilamentNotificationServiceProvider
bash
php artisan migrate
bash
php artisan make:notification YOUR_NOTIFICATION_NAME