tkeer/flattable
tkeer/flattable flattens nested arrays and Laravel/Eloquent data into clean, single-level rows for HTML tables, CSV exports, and reports. It simplifies turning complex hierarchical structures into readable tabular output with minimal setup, making it useful for dashboards, exports, and data-heavy ad...
composer require tkeer/flattable, then publish the package config (php artisan vendor:publish --provider="Tkeer\Flattable\FlattableServiceProvider"). Open config/flattable.php first — it's the single place that wires source models to flat models, so it's your map for the whole package.Schema::create — add every column you intend to duplicate (e.g. name, city_name, order_count) and index the columns you'll filter or sort on.Flattable trait and exposes a config array describing the source model, a columns map (source_column => flat_column), and any relations to pull through (e.g. city.name → city_name).First use case: a listing/read table. You have orders joining users, addresses, and order_items; you build flat_orders with the few columns your index page needs so the list query is a single indexed SELECT.
belongsTo fields (customer name, city) and hasMany aggregates (item count, total). Aggregates are the biggest win — replace withCount/sum subqueries on the list page with a stored column.flat_orders, register observers on Order, OrderItem, User, and Address. When any of them changes, the corresponding flat rows should be updated. Model this explicitly: "which source rows touch this flat row?"Address but nothing happens to flat_orders because the address model isn't in the config. Audit the flat model's dependency list whenever you add a column.updateOrCreate in batches rather than letting per-row observers fire.php artisan config:cache in production means the model/observer wiring is frozen; changes to config/flattable.php need a rebuild and deploy. Clear config when debugging "observer doesn't fire locally."WHERE/ORDER BY clauses, and check with EXPLAIN.RefreshDatabase and either seed the flat table directly or trigger the sync helper, rather than relying on observers fired from fixtures you don't control.flattable:rebuild style command; when sync logic drifts or a migration changes columns, being able to regenerate from source is what makes the approach safe to maintain.How can I help you explore Laravel packages today?