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 Generator Laravel Package

yeejiawei/laravel-table-generator

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require yeejiawei/laravel-table-generator
    

    Publish the config (if needed) with:

    php artisan vendor:publish --provider="YeeJiaWei\LaravelTableGenerator\TableGeneratorServiceProvider"
    
  2. First Use Case: Generate a basic table for a User model with pagination:

    use YeeJiaWei\LaravelTableGenerator\Facades\TableGenerator;
    
    $users = User::paginate(10);
    return TableGenerator::create($users)
        ->setTableName('Users')
        ->addColumn('id')
        ->addColumn('name')
        ->addColumn('email')
        ->render();
    
  3. Where to Look First:

    • Review the usage examples in the README.
    • Check the config/table-generator.php for customizable defaults (e.g., column types, styling).
    • Inspect the TableGenerator facade methods in src/Facades/TableGenerator.php for available chainable methods.

Implementation Patterns

Core Workflow

  1. Data Binding: Pass a Laravel collection or paginator (e.g., Model::paginate()) to TableGenerator::create($data).

  2. Column Configuration: Use fluent methods to define columns:

    ->addColumn('id', ['sortable' => true])
    ->addColumn('name', ['searchable' => true, 'width' => '20%'])
    ->addColumn('status', ['type' => 'select', 'options' => ['active' => 'Active', 'inactive' => 'Inactive']])
    
  3. Action Buttons: Attach CRUD actions with route names or URLs:

    ->setCreatable('users.create')
    ->setViewable('users.show')
    ->setEditable('users.edit')
    ->setDeletable('users.destroy')
    ->setEnable('users.toggle-status')
    
  4. Timestamps: Add created/updated columns with:

    ->addCreatedAtColumns(['label' => 'Created On'])
    ->addUpdatedAtColumns(['label' => 'Last Updated'])
    
  5. Rendering: Output the table in a Blade view or return raw HTML:

    // Blade view
    @include('table-generator::table', ['table' => $table])
    
    // Raw HTML (e.g., for APIs)
    echo $table->render();
    

Integration Tips

  • Blade Views: Use the @include directive with the package's view path (table-generator::table). Customize the view by publishing it first:

    php artisan vendor:publish --tag=table-generator-views
    
  • API Responses: For APIs, return the rendered HTML or a structured array:

    return response()->json(['table' => $table->toArray()]);
    
  • Dynamic Columns: Fetch columns dynamically from a model's $fillable or accessors:

    $columns = collect($model->fillable)->map(fn($field) => $field)->toArray();
    $table->addColumns($columns);
    
  • Styling: Override CSS/JS by publishing assets:

    php artisan vendor:publish --tag=table-generator-assets
    

    Then extend the default styles in your app's assets.

  • Localization: Customize labels/buttons via the config:

    'buttons' => [
        'create' => __('Create New'),
        'edit'   => __('Edit'),
        // ...
    ],
    

Gotchas and Tips

Pitfalls

  1. Archived Package:

    • The package is archived (last release in 2021). Test thoroughly in staging before production use.
    • Consider forking the repo if you need active maintenance.
  2. Pagination Issues:

    • Ensure you pass a paginator (e.g., Model::paginate()), not a raw collection, for pagination controls to work.
    • Custom pagination logic may require overriding the render() method.
  3. Column Type Mismatches:

    • The package assumes basic column types (text, select, etc.). Complex data (e.g., JSON) may need custom rendering:
      ->addColumn('metadata', function($row) {
          return json_encode($row->metadata);
      })
      
  4. Route Binding:

    • Action buttons (e.g., setEditable) expect route names, not URLs. Use route('users.edit', $user) in Blade if needed.
  5. CSRF Tokens:

    • Form actions (e.g., delete) require CSRF protection. Ensure your Blade layout includes @csrf.
  6. JavaScript Dependencies:

    • The package may rely on jQuery or Bootstrap. Verify compatibility with your project’s assets.

Debugging

  1. Inspect the Table Object: Dump the table configuration before rendering:

    dd($table->getColumns(), $table->getOptions());
    
  2. Check Published Views: If the table doesn’t render, verify the published view (resources/views/vendor/table-generator/table.blade.php) exists and is correctly included.

  3. Log Configuration: Add debug logs to config/table-generator.php:

    'debug' => env('TABLE_GENERATOR_DEBUG', false),
    

    Then check Laravel logs for package activity.

Tips

  1. Extend Functionality: Create a custom table builder by extending the YeeJiaWei\LaravelTableGenerator\TableGenerator class:

    namespace App\Services;
    
    use YeeJiaWei\LaravelTableGenerator\TableGenerator as BaseTable;
    
    class CustomTable extends BaseTable {
        public function addCustomColumn($field) {
            $this->addColumn($field, ['type' => 'custom']);
            // Add logic here
        }
    }
    
  2. Conditional Columns: Hide/show columns based on user roles:

    ->addColumn('admin_only_field', [
        'visible' => function($row) {
            return auth()->user()->isAdmin();
        }
    ])
    
  3. Bulk Actions: Add bulk select/destroy functionality by extending the table’s JavaScript:

    // Publish assets first, then extend in your app.js
    $(document).ready(function() {
        $('.table-generator').on('bulkAction', function() {
            // Custom logic
        });
    });
    
  4. Performance:

    • For large datasets, use ->setPerPage(50) or implement server-side processing.
    • Lazy-load columns if many are defined.
  5. Testing: Mock the TableGenerator facade in tests:

    $this->partialMock(TableGenerator::class, function($mock) {
        $mock->shouldReceive('render')->andReturn('<table>...</table>');
    });
    
  6. Fallback for Missing Data: Handle null values in columns:

    ->addColumn('description', ['fallback' => 'N/A'])
    
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.
nasirkhan/laravel-sharekit
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony