mukadi/chartjs-builder
mukadi/chartjs-builder offers a fluent PHP/Laravel API for building Chart.js charts. Define datasets, labels, and options in PHP, then output chart-ready JSON or JavaScript, reducing manual array wrangling and keeping chart configuration reusable and easier to test.
Install with Composer:
composer require mukadi/chartjs-builder
The package sits on top of Chart.js — you hand it a SQL query and it turns the result set into a Chart.js config object you can render server-side or return as JSON. Start by reading the ChartBuilder class and the README examples; the public surface is only a handful of classes, so it's readable in one sitting.
Minimal flow in plain PHP:
use Mukadi\Chart\ChartBuilder;
$builder = new ChartBuilder($pdo);
$chart = $builder->build("SELECT status AS label, COUNT(*) AS value FROM orders GROUP BY status");
echo $chart->render(); // inline <canvas> + script
// or $chart->toArray() // Chart.js config for JSON responses
Your first real use case: any GROUP BY that returns a category plus a count (status breakdowns, signups per month, top products). Alias the columns to the label/value pair the builder expects and it's a one-liner.
In Laravel the service provider and facade are auto-discovered; the published config is where you point it at a non-default DB connection.
$chart->toArray() from a controller and instantiate new Chart(ctx, config) client-side. This is the cleanest integration and avoids server-rendered script tags entirely.<canvas> yourself, then push the config with @push('scripts') and @json($config). You keep layout control and avoid output-order surprises.? placeholders in the raw SQL; the builder executes it verbatim, so interpolating user input is an injection vector.NULL alias, a driver that returns strings instead of ints) render as blank canvases rather than errors. Assert on toArray() in tests.MONTH()), Postgres (DATE_TRUNC()), and SQLite (strftime()). Nothing is normalized for you; the query is yours.@json() the config in Blade. Hand-json_encode-ing inside a <script> tag produces quoting bugs that are painful to debug.data.labels, data.datasets) instead of snapshotting HTML. It survives package upgrades far better.ChartBuilder) beats forking if you need a custom dataset format.How can I help you explore Laravel packages today?