Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Table Laravel Package

okipa/laravel-table

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require okipa/laravel-table
    

    Publish the config file (if needed):

    php artisan vendor:publish --provider="Okipa\LaravelTable\TableServiceProvider"
    
  2. Basic Usage: Generate a table for an Eloquent model (e.g., User) in a controller or blade view:

    use Okipa\LaravelTable\Table;
    
    $table = new Table(User::query());
    $table->render();
    

    Or directly in Blade:

    @table(User::query())
    
  3. First Use Case: Quickly scaffold a CRUD table for a model without writing repetitive code:

    $table = new Table(User::query())
        ->setColumns(['id', 'name', 'email', 'created_at'])
        ->setSearchable(['name', 'email'])
        ->setSortable(['name', 'email']);
    

Implementation Patterns

Core Workflows

  1. Dynamic Table Generation:

    • Use Table::make() or Table::forModel() for model-based tables:
      $table = Table::forModel(User::class)->paginate(10);
      
    • Chain methods for configuration:
      $table->setColumns(['id', 'name', 'email'])
            ->setSearchable(['name'])
            ->setActions(['edit', 'delete']);
      
  2. Blade Integration:

    • Embed tables in views with @table directive:
      @table(User::query(), ['columns' => ['id', 'name']])
      
    • Customize columns dynamically:
      @table(User::query())
          @column('name', 'Full Name')
          @column('email', 'Email Address')
      @endtable
      
  3. API/JSON Output:

    • Return table data as JSON for APIs:
      return response()->json($table->toArray());
      
  4. Custom Actions:

    • Define custom action buttons:
      $table->addAction('verify', 'Verify', function ($user) {
          return route('users.verify', $user);
      });
      

Advanced Patterns

  1. Relationships:

    • Display related model data:
      $table->addColumn('posts_count', function ($user) {
          return $user->posts()->count();
      });
      
  2. Conditional Columns:

    • Show/hide columns based on logic:
      $table->addColumn('status', function ($user) {
          return $user->is_active ? 'Active' : 'Inactive';
      })->setVisible(function ($user) {
          return auth()->user()->isAdmin();
      });
      
  3. Bulk Actions:

    • Add bulk operations:
      $table->addBulkAction('delete', 'Delete Selected', function ($users) {
          $users->delete();
      });
      
  4. Exporting:

    • Export table data to CSV/Excel:
      $table->export('csv', 'users.csv');
      

Gotchas and Tips

Common Pitfalls

  1. Performance:

    • Issue: Eager-loading relationships is critical for large datasets. Forgetting with() can cause N+1 queries.
    • Fix: Always eager-load relationships:
      $table = new Table(User::query()->with('posts'));
      
  2. Column Naming:

    • Issue: Column names in Blade directives must match the model's fillable attributes or accessors.
    • Fix: Use setColumn() with custom labels:
      $table->setColumn('name', 'Full Name', 'name');
      
  3. Search/Sort Conflicts:

    • Issue: Searchable/sortable columns must exist in the database or be computed columns.
    • Fix: Validate columns before setting:
      if (Schema::hasColumn('users', 'email')) {
          $table->setSearchable(['email']);
      }
      
  4. Pagination:

    • Issue: Forgetting to paginate large datasets can crash the server.
    • Fix: Always paginate:
      $table->paginate(25);
      

Debugging Tips

  1. Log Queries: Enable Laravel's query logging to debug slow tables:

    DB::enableQueryLog();
    $table->render();
    dd(DB::getQueryLog());
    
  2. Check Config:

    • Verify config/table.php for global settings like default columns or actions.
  3. Blade Cache:

    • Clear Blade cache if tables render incorrectly:
      php artisan view:clear
      

Extension Points

  1. Custom Renderers:

    • Override the default renderer:
      $table->setRenderer(new CustomRenderer());
      
  2. Event Hooks:

    • Listen for table events (e.g., table.rendering):
      event(new TableRendering($table));
      
  3. Service Provider:

    • Extend the package via the TableServiceProvider:
      public function boot()
      {
          Table::macro('customMethod', function () {
              // Logic
          });
      }
      
  4. Tailwind CSS:

    • Customize styles by overriding the default Tailwind classes in your CSS:
      .laravel-table th { background: #f8f9fa; }
      
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
daikazu/eloquent-salesforce-objects
unseen-codes/chat
romalytar/yammi-jobs-monitoring-laravel
kisame76/filament-db-table-state
nqxcode/laravel-lucene-search
dpfx/laravel-livewire-wizards
workos/workos-php-laravel
sofa/laravel-global-scope
nawasara/auth-primitives
adhocrat-io/arkhe-main
make-dev/orca-harpoon
itsemon245/lamet
baks-dev/dashboard
amoifr/pickle-panther-bundle
make-dev/orca
dmstr/symfony-system-resources-bundle
dmstr/symfony-job-queue-bundle
dmstr/openapi-json-schema-bundle
dmstr/keycloak-security-bundle
dmstr/doctrine-audit-log-bundle