Laravel Filament: Creating Dynamic Tables

In Laravel Filament, resources are a fundamental building block for creating, managing, and interacting with your application’s data. Resources define how data is displayed and edited within the Filament admin panel. In this article, we will explore a code example that defines a Filament resource for managing attendance logs using the AttendancelogsResource class. This resource includes a form and a dynamic table to display attendance records based on certain conditions.

Use Case on how create Dynamic Tables With Laravel Filament

Let’s say you have multiple roles and want to limit the data in the table for the assigned role.

In this example, the Security role will be unable to perform CRUD operations for logged data from the previous days.

Defining the AttendancelogsResource Class

The AttendancelogsResource class is responsible for specifying how attendance logs are managed within the Filament admin panel. It consists of a form and a table definition, each serving a distinct purpose.

Form Definition

The form method within the AttendancelogsResource class defines the structure and behaviour of the form used for creating and editing attendance records. In this code example, we have omitted the form’s schema for brevity, but you can customize it to include the fields you need. Here’s how the form method is defined:

				
					public static function form(Form $form): Form
{
    return $form
        ->schema([
            // Define the form fields here
        ]);
}
				
			

In this method, you would typically define form fields like text inputs, date pickers, and checkboxes, depending on your application’s requirements.

Dynamic Table Definition

The table method within the AttendancelogsResource class defines the behaviour and structure of the dynamic table used to display attendance records. This table includes columns for displaying data, and its behaviour can be customized to filter data based on certain conditions. Here’s how the table method is defined as:

				
					public static function table(Table $table): Table
{
    return $table
        ->query(function () {
            $date = now(); // Specify the desired date

            if(auth()->user()->hasRole(['Security'])) {
                return Attendancelogs::query()
                    ->whereDate('date_in', $date);
            }

            return Attendancelogs::query();
        })
        ->columns([
            // Define your columns here
            Column::make('date_in')
                ->label('Date In')
                ->sortable()
                ->searchable(),
            // Add more columns as needed
        ]);
}
				
			

In this method, we define the behaviour of the dynamic table:

  • The query closure specifies how data should be queried. In this example, it checks if the authenticated user has the ‘Security’ role. If they do, it filters attendance logs to include only those with a date_in matching the specified date.
  • The columns method defines the columns to display in the table. In this example, we have a single column, ‘Date In,’ which is sortable and searchable. You can add more columns as needed, such as ‘Employee Name’ or ‘Attendance Status.’

Here’s a the complete example of how you might use it

				
					<?php

namespace App\Filament\Resources;

use App\Models\Attendancelogs;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Carbon;

class AttendancelogsResource extends Resource
{
    protected static ?string $model = Attendancelogs::class;

    public static function form(Form $form): Form
    {
        return $form
        ->schema([
            // Add form content
        ]);
    }

   public static function table(Table $table): Table
    {
        return $table
            ->query(function () {
                $date = now(); // Specify the desired date

                if(auth()->user()->hasRole(['Security'])) {
                    return Attendancelogs::query()
                    ->whereDate('date_in', $date);
                }

                return Attendancelogs::query();
            })


            ->columns([
                // Define your columns here
                // For example, to display the date_in column:
                Column::make('date_in')
                    ->label('Date In')
                    ->sortable()
                    ->searchable(),

            // Add more columns as needed
        ]);
    }
				
			

In summary, Laravel Filament makes it easy to create dynamic tables to display and manage data in your admin panel. By defining a table class and specifying the data source and columns, you can easily create customized tables tailored to your application’s needs.

This page may contain affiliate links, which help support our project. Read our affiliate disclosure.