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

medcharrafi's avatar

pivot table , query optimization

Hello Laravel community,

I am currently working on a project management system and facing a performance challenge in the implementation of a Filament Table Builder. In my application, I have a many-to-many relationship between employees and tasks. The tasks table includes a column named "manager_suggested_time," which is based on the role of each employee (e.g., a manager might suggest 100 hours for effective work and 50 hours for assistance).

Employees enter data about their work, specifying the duration they spent on a task and the role they performed. I want to display this information, along with the actual time each task took based on employee roles, to the I am using Filament's Table Builder for this mission .

I have implemented the code, but it generates more than 14 queries and consumes over 40MB of memory. I'm seeking advice on how to optimize the code for better performance. Here is a snippet of the current code:

       Tables\Columns\TextColumn::make('assistant time (hours)')
                ->getStateUsing(function (Task $record): string {
                    $totalSpentHours = 0;
                    if ($record->employees) {
                        foreach ($record->employees as $employee) {
                            $role = $employee->pivot->role;
                            $spentHours = $employee->pivot->spent_hours;
                            if ($role === 2) {
                                $totalSpentHours += $spentHours;
                            }
                        }
                    }
                    return (string) $totalSpentHours;
                })
                ->sortable(),
            Tables\Columns\TextColumn::make('effective time (hours)')
                ->getStateUsing(function (Task $record): string {
                    $totalSpentHours = 0;
                    if ($record->employees) {
                        foreach ($record->employees as $employee) {
                            $role = $employee->pivot->role;
                            $spentHours = $employee->pivot->spent_hours;
                            if ($role === 1) {
                                $totalSpentHours += $spentHours;
                            }
                        }
                    }
                    return (string) $totalSpentHours;
                })
                ->sortable(), 

it generate more than 14 querie and more than 40MB memory usage and this is the setup of the relationship

public function employees(): BelongsToMany
{
    return self::belongsToMany(
        User::class,
        table: 'task_user',
        foreignPivotKey: 'user_id',
        relatedPivotKey: 'task_id'
    )->withTimestamps()
        ->withPivot('role', 'spent_hours');
}
0 likes
2 replies
medcharrafi's avatar

i am searching also if there is completely any better way than using Tables\Columns\TextColumn::make('effective time (hours)') ->getStateUsing(function (Task $record): string { cz that two column effective time (hours) and assistant time (hours ) is custom

in filament cz does not exists in the docs of filament v3

Please or to participate in this conversation.