Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

LawrenceSmith's avatar

Where to place logic in Filament

Hi Everyone

I have an application that is made up of a couple of Groovy scripts that read data from a DB, does some calculations and then updates another DB with the modified data. This is then shown in a PHP file using a plain HTML table.

All the scripting happens in the background using a Groovy timer so there’s no user intervention.

I’m in the process of refactoring all of this into Filament but I’m not sure where / how I should put the logic that the Groovy scripts process. I’ve considered “actions” in a view page as well as creating a custom Livewire page, but that seems a bit overkill for the requirements.

Any thoughts or suggestions are very much appreciated.

Thanks

0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To refactor your application into Filament, you need to consider where to place the business logic that was previously handled by your Groovy scripts. Filament is built on top of Laravel and Livewire, so you have several options for organizing your code. Here are some suggestions:

  1. Service Classes: This is a good place to encapsulate your business logic. You can create a service class that handles the data processing and calculations. This keeps your code clean and separates the business logic from the presentation layer.

  2. Livewire Components: If you need real-time interactivity, you can use Livewire components. However, for your use case, it might be overkill unless you need dynamic updates on the frontend.

  3. Actions: Filament actions can be used for specific tasks, but they are generally more suited for user-triggered events rather than background processing.

  4. Jobs: If your data processing is time-consuming, consider using Laravel Jobs to handle the processing in the background.

Here is an example of how you might structure your service class and use it within a Filament resource:

Step 1: Create a Service Class

Create a service class to handle the data processing logic.

// app/Services/DataProcessingService.php

namespace App\Services;

use Illuminate\Support\Facades\DB;

class DataProcessingService
{
    public function processData()
    {
        // Read data from the DB
        $data = DB::table('source_table')->get();

        // Perform calculations
        $processedData = $data->map(function ($item) {
            // Your calculation logic here
            $item->calculated_field = $item->original_field * 2; // Example calculation
            return $item;
        });

        // Update another DB with the modified data
        foreach ($processedData as $item) {
            DB::table('destination_table')->updateOrInsert(
                ['id' => $item->id],
                (array) $item
            );
        }
    }
}

Step 2: Use the Service in a Filament Resource

You can call this service from a Filament resource or a custom page.

Step 3: Trigger the Processing

You can trigger the data processing from a custom action or a button in your Filament resource.

// app/Filament/Resources/YourResource/Pages/ListYourModels.php

namespace App\Filament\Resources\YourResource\Pages;

use App\Filament\Resources\YourResource;
use Filament\Resources\Pages\ListRecords;
use Filament\Pages\Actions\ButtonAction;

class ListYourModels extends ListRecords
{
    protected static string $resource = YourResource::class;

    protected function getActions(): array
    {
        return [
            ButtonAction::make('Process Data')
                ->action('processData')
                ->color('primary'),
        ];
    }

    public function processData()
    {
        YourResource::processData();
        $this->notify('success', 'Data processed successfully!');
    }
}

This setup keeps your business logic encapsulated in a service class and allows you to trigger the processing from your Filament resource. This approach is clean, maintainable, and leverages Laravel's powerful features.

LawrenceSmith's avatar

How do I trigger a service class without user interaction

Please or to participate in this conversation.