PHP code example of tomatophp / filament-alerts

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

    

tomatophp / filament-alerts example snippets


->plugin(\TomatoPHP\FilamentAlerts\FilamentAlertsPlugin::make()
)



namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;
use TomatoPHP\FilamentAlerts\Traits\InteractsWithNotifications;

class User extends Authenticatable
{
    use HasApiTokens;
    use HasFactory;
    use HasProfilePhoto;
    use Notifiable;
    use TwoFactorAuthenticatable;
    use HasRoles;
    use InteractsWithNotifications;
    ...

use Filament\Notifications\Notification;

Notification::make('send')
    ->title('Test Notifications')
    ->body('This is a test notification')
    ->icon('heroicon-o-bell')
    ->color('success')
    ->actions([
        \Filament\Notifications\Actions\Action::make('view')
            ->label('View')
            ->url('https://google.com')
            ->markAsRead()
    ])
    ->sendUse(auth()->user(), \TomatoPHP\FilamentAlerts\Services\Drivers\EmailDriver::class);
  

use TomatoPHP\FilamentAlerts\Facades\FilamentAlerts;

FilamentAlerts::notify(User::first())
    ->template($template->id)
    ->title([
        "find-text" => "change with this"
    ])
    ->body([
        "find-text" => "change with this"
    ])
    ->send();

$user->notifyEmail(string $message, ?string $subject = null, ?string $url = null);
$user->notifyDB(string $message, ?string $title=null, ?string $url =null);

->plugin(\TomatoPHP\FilamentAlerts\FilamentAlertsPlugin::make()
    ->hideNotificationsResources()
)

->plugin(\TomatoPHP\FilamentAlerts\FilamentAlertsPlugin::make()
    ->useSettingsHub()
)



namespace TomatoPHP\FilamentAlerts\Services\Drivers;

use TomatoPHP\FilamentAlerts\Jobs\NotifyDatabaseJob;

class DatabaseDriver extends Driver
{
    public function setup(): void
    {
        // TODO: Implement setup() method.
    }

    public function sendIt(
        string $title,
        string $model,
        int | string | null $modelId = null,
        ?string $body = null,
        ?string $url = null,
        ?string $icon = null,
        ?string $image = null,
        ?string $type = 'info',
        ?string $action = 'system',
        ?array $data = [],
        ?int $template_id = null,
    ): void {
        if ($modelId) {
            dispatch(new NotifyDatabaseJob([
                'model_type' => $model,
                'modelId' => $modelId,
                'title' => $title,
                'body' => $body,
                'url' => $url,
                'icon' => $icon,
                'type' => $type,
                'action' => $action,
                'data' => $data,
                'template_id' => $template_id,
            ]));
        } else {
            foreach ($model::all() as $user) {
                dispatch(new NotifyDatabaseJob([
                    'model_type' => $model,
                    'modelId' => $user->id,
                    'title' => $title,
                    'body' => $body,
                    'url' => $url,
                    'icon' => $icon,
                    'type' => $type,
                    'action' => $action,
                    'data' => $data,
                    'template_id' => $template_id,
                ]));
            }
        }

    }
}


use TomatoPHP\FilamentAlerts\Facades\FilamentAlerts;

public function boot() {
    FilamentAlerts::register(
        \TomatoPHP\FilamentAlerts\Services\Concerns\NotificationDriver::make('database')
            ->label('Database')
            ->color('primary')
            ->icon('heroicon-o-bell')
            ->driver(\TomatoPHP\FilamentAlerts\Services\Drivers\DatabaseDriver::class)
    );
} 

use TomatoPHP\FilamentAlerts\Facades\FilamentAlerts;

public function boot() {
    FilamentAlerts::register(
        \TomatoPHP\FilamentAlerts\Services\Concerns\NotificationType::make('system')
            ->label('System')
            ->color('primary')
            ->icon('heroicon-o-bell')
    );
} 

use TomatoPHP\FilamentAlerts\Facades\FilamentAlerts;

public function boot() {
    FilamentAlerts::register(
        \TomatoPHP\FilamentAlerts\Services\Concerns\NotificationAction::make('system')
            ->label('System')
            ->color('primary')
            ->icon('heroicon-o-bell')
    );
} 

use TomatoPHP\FilamentAlerts\Facades\FilamentAlerts;

public function boot() {
    FilamentAlerts::register(
        \TomatoPHP\FilamentAlerts\Services\Concerns\NotificationUser::make(User::class)
            ->label('User')
            ->color('primary')
            ->icon('heroicon-o-bell')
    );
} 

use TomatoPHP\FilamentAlerts\Filament\Resources\NotificationsTemplateResource\Table\NotificationsTemplateTable;

public function boot()
{
    NotificationsTemplateTable::register([
        \Filament\Tables\Columns\TextColumn::make('something')
    ]);
}

use TomatoPHP\FilamentAlerts\Filament\Resources\NotificationsTemplateResource\Table\NotificationsTemplateActions;

public function boot()
{
    NotificationsTemplateActions::register([
        \Filament\Tables\Actions\ReplicateAction::make()
    ]);
}

use TomatoPHP\FilamentAlerts\Filament\Resources\NotificationsTemplateResource\Table\NotificationsTemplateFilters;

public function boot()
{
    NotificationsTemplateFilters::register([
        \Filament\Tables\Filters\SelectFilter::make('something')
    ]);
}

use TomatoPHP\FilamentAlerts\Filament\Resources\NotificationsTemplateResource\Table\NotificationsTemplateBulkActions;

public function boot()
{
    NotificationsTemplateBulkActions::register([
        \Filament\Tables\BulkActions\DeleteAction::make()
    ]);
}

use TomatoPHP\FilamentAlerts\Filament\Resources\NotificationsTemplateResource\Form\NotificationsTemplateForm;

public function boot()
{
    NotificationsTemplateForm::register([
        \Filament\Forms\Components\TextInput::make('something')
    ]);
}

use TomatoPHP\FilamentAlerts\Filament\Resources\NotificationsTemplateResource\Actions\ManagePageActions;
use TomatoPHP\FilamentAlerts\Filament\Resources\NotificationsTemplateResource\Actions\EditPageActions;
use TomatoPHP\FilamentAlerts\Filament\Resources\NotificationsTemplateResource\Actions\ViewPageActions;
use TomatoPHP\FilamentAlerts\Filament\Resources\NotificationsTemplateResource\Actions\CreatePageActions;

public function boot()
{
    ManagePageActions::register([
        Filament\Actions\Action::make('action')
    ]);
    
    EditPageActions::register([
        Filament\Actions\Action::make('action')
    ]);
    
    ViewPageActions::register([
        Filament\Actions\Action::make('action')
    ]);
    
    CreatePageActions::register([
        Filament\Actions\Action::make('action')
    ]);
}

use TomatoPHP\FilamentAlerts\Filament\Resources\NotificationsTemplateResource\Infolist\NotificationsTemplateInfoList;

public function boot()
{
    NotificationsTemplateInfoList::register([
       \Filament\Infolists\Components\TextEntry::make('something')
    ]);
}

/**
 * ---------------------------------------------
 * Resource Building
 * ---------------------------------------------
 * if you want to use the resource custom class
 */
'resource' => [
    'table' => [
        'class' => \TomatoPHP\FilamentAlerts\Filament\Resources\NotificationsTemplateResource\Table\NotificationsTemplateTable::class,
        'filters' => \TomatoPHP\FilamentAlerts\Filament\Resources\NotificationsTemplateResource\Table\NotificationsTemplateFilters::class,
        'actions' => \TomatoPHP\FilamentAlerts\Filament\Resources\NotificationsTemplateResource\Table\NotificationsTemplateActions::class,
        'header-actions' => \TomatoPHP\FilamentAlerts\Filament\Resources\NotificationsTemplateResource\Table\NotificationsTemplateHeaderActions::class,
        'bulkActions' => \TomatoPHP\FilamentAlerts\Filament\Resources\NotificationsTemplateResource\Table\NotificationsTemplateBulkActions::class,
    ],
    'form' => [
        'class' => \TomatoPHP\FilamentAlerts\Filament\Resources\NotificationsTemplateResource\Form\NotificationsTemplateForm::class,
    ],
    'infolist' => [
        'class' => \TomatoPHP\FilamentAlerts\Filament\Resources\NotificationsTemplateResource\Infolist\NotificationsTemplateInfoList::class,
    ],
    'pages' => [
        'list' => \TomatoPHP\FilamentAlerts\Filament\Resources\NotificationsTemplateResource\Actions\ManagePageActions::class,
        'create' => \TomatoPHP\FilamentAlerts\Filament\Resources\NotificationsTemplateResource\Actions\CreatePageActions::class,
        'edit' => \TomatoPHP\FilamentAlerts\Filament\Resources\NotificationsTemplateResource\Actions\EditPageActions::class,
        'view' => \TomatoPHP\FilamentAlerts\Filament\Resources\NotificationsTemplateResource\Actions\ViewPageActions::class,
    ],
]
bash
php artisan filament-alerts:install
bash
php artisan queue:work
bash
php artisan vendor:publish --tag="filament-alerts-config"
bash
php artisan vendor:publish --tag="filament-alerts-views"
bash
php artisan vendor:publish --tag="filament-alerts-lang"
bash
php artisan vendor:publish --tag="filament-alerts-migrations"
bash
composer analyse