PHP code example of xplodman / filamentapproval

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

    

xplodman / filamentapproval example snippets


// app/Providers/Filament/AdminPanelProvider.php
use Filament\Panel;
use Filament\PanelProvider;
use Xplodman\FilamentApproval\FilamentApprovalPlugin;

class AdminPanelProvider extends PanelProvider
{
    public function panel(Panel $panel): Panel
    {
        return $panel
            ->plugins([
                FilamentApprovalPlugin::make(),
            ]);
    }
}

return [
    /*
    |--------------------------------------------------------------------------
    | User Model
    |--------------------------------------------------------------------------
    |
    | The user model class that will be used for relationships in the
    | approval requests. This should match your application's user model.
    |
    */
    'user_model' => config('auth.providers.users.model', 'App\Models\User'),

    /*
    |--------------------------------------------------------------------------
    | Approval Request Model
    |--------------------------------------------------------------------------
    |
    | The model class that will be used for approval requests.
    | You can override this to use a custom model (e.g., MongoDB model).
    |
    */
    'approval_request_model' => \Xplodman\FilamentApproval\Models\ApprovalRequest::class,

    /*
    |--------------------------------------------------------------------------
    | Navigation Group
    |--------------------------------------------------------------------------
    |
    | The navigation group where the approval requests resource will appear
    | in the Filament admin panel.
    |
    */
    'navigation_group' => 'Management',

    /*
    |--------------------------------------------------------------------------
    | Navigation Icon
    |--------------------------------------------------------------------------
    |
    | The icon that will be displayed next to the approval requests resource
    | in the Filament admin panel navigation.
    |
    */
    'navigation_icon' => 'heroicon-o-clipboard-document-check',

    /*
    |--------------------------------------------------------------------------
    | Auto Register Resource
    |--------------------------------------------------------------------------
    |
    | Whether to automatically register the ApprovalRequestResource with
    | Filament panels. Set to true if you want automatic registration.
    | For most cases, you should manually register it in your panel.
    |
    */
    'auto_register_resource' => false,

    /*
    |--------------------------------------------------------------------------
    | Debug Mode
    |--------------------------------------------------------------------------
    |
    | Enable debug mode to show detailed debug information in the diff view.
    | This 



namespace App\Filament\Resources\PostResource\Pages;

use App\Filament\Resources\PostResource;
use Filament\Resources\Pages\CreateRecord;
use Xplodman\FilamentApproval\Concerns\InterceptsCreateForApproval;

class CreatePost extends CreateRecord
{
    use InterceptsCreateForApproval;
    
    protected static string $resource = PostResource::class;
}



namespace App\Filament\Resources\PostResource\Pages;

use App\Filament\Resources\PostResource;
use Filament\Resources\Pages\EditRecord;
use Xplodman\FilamentApproval\Concerns\InterceptsEditForApproval;

class EditPost extends EditRecord
{
    use InterceptsEditForApproval;
    
    protected static string $resource = PostResource::class;
}



namespace App\Filament\Resources;

use App\Filament\Resources\PostResource\Pages\CreatePost;
use App\Filament\Resources\PostResource\Pages\EditPost;
use App\Filament\Resources\PostResource\Pages\ListPosts;
use Filament\Resources\Resource;
use Xplodman\FilamentApproval\Enums\RelationTypeEnum;

class PostResource extends Resource
{
    protected static ?string $model = Post::class;

    public static function getPages(): array
    {
        return [
            'index' => ListPosts::route('/'),
            'create' => CreatePost::route('/create'),
            'edit' => EditPost::route('/{record}/edit'),
        ];
    }

    /**
     * Define approval relations for relationship handling
     * This method is 

use Xplodman\FilamentApproval\Enums\RelationTypeEnum;

public static function approvalRelations(): array
{
    return [
        // Belongs to relationships
        'author' => ['type' => RelationTypeEnum::BELONGS_TO->value, 'field' => 'author_id'],
        'category' => ['type' => RelationTypeEnum::BELONGS_TO->value, 'field' => 'category_id'],
        'status' => ['type' => RelationTypeEnum::BELONGS_TO->value, 'field' => 'status_id'],
        
        // Many-to-many relationships
        'tags' => ['type' => RelationTypeEnum::BELONGS_TO_MANY->value, 'field' => 'tag_ids'],
        'categories' => ['type' => RelationTypeEnum::BELONGS_TO_MANY->value, 'field' => 'category_ids'],
        
        // One-to-many relationships
        'comments' => ['type' => RelationTypeEnum::HAS_MANY->value, 'field' => 'comment_ids'],
        'attachments' => ['type' => RelationTypeEnum::HAS_MANY->value, 'field' => 'attachment_ids'],
    ];
}

// config/filamentapproval.php
'auto_register_resource' => true,

// In your panel provider or service provider
use Xplodman\FilamentApproval\Resources\ApprovalRequestResource;

$panel->resources([
    ApprovalRequestResource::class,
    // ... your other resources
]);

// config/shield.php
'custom_permissions' => [
    'approve_approval_requests',
    'reject_approval_requests', 
    'bypass_approval_requests',
],

// config/filamentapproval.php
'permissions' => [
    'approve' => 'approve_approval_requests',
    'reject' => 'reject_approval_requests',
    'bypass' => 'bypass_approval_requests',
],

// Create permissions
Permission::create(['name' => 'approve_approval_requests']);
Permission::create(['name' => 'reject_approval_requests']);
Permission::create(['name' => 'bypass_approval_requests']);

// Assign to roles
$adminRole->givePermissionTo([
    'approve_approval_requests',
    'reject_approval_requests',
    'bypass_approval_requests'
]);
bash
php artisan vendor:publish --tag="filamentapproval-config"
bash
php artisan vendor:publish --tag="filamentapproval-migrations"
php artisan migrate