There are several Laravel packages and libraries that allow you to generate charts and graphs from SQL queries, though none provide a fully automated solution where you simply input a SQL query and get a graph without any additional configuration. However, some options come close to this functionality:
Laravel Charts
Laravel Charts is a popular package that allows you to create charts from database queries. While it doesn't directly accept raw SQL, it provides a simple API to generate charts from Eloquent queries, which can be based on raw SQL[25].
Example usage:
$chart = Charts::database(User::all(), 'bar', 'highcharts')
->setElementLabel("Total")
->setDimensions(1000, 500)
->setResponsive(false)
->groupByDay();
Larapex Charts
Larapex Charts is another package that simplifies chart creation in Laravel. It doesn't directly accept SQL queries, but it allows you to easily create charts from data retrieved via Eloquent or the Query Builder[24].
Example:
$chart = LarapexChart::setTitle('Your Todos Stats')
->setLabels(['Done', 'Not Yet'])
->setDataset([$todosDone, $todosNotYet]);
ConsoleTVs/Charts
This package supports multiple charting libraries and allows you to create charts from database queries. While it doesn't directly accept raw SQL, you can use Laravel's query builder to construct your query and then pass the results to the chart[7].
Custom Solution
For a more automated approach, you could create a custom solution that combines Laravel's raw SQL capabilities with a charting library:
- Use DB::select() to run your raw SQL query[23].
- Process the results into a format suitable for your chosen charting library.
- Use a JavaScript charting library like Chart.js or ApexCharts to render the chart on the frontend.
Here's a basic example of how this might work:
$results = DB::select('YOUR RAW SQL QUERY HERE');
// Process results into chart data format
return view('chart', ['chartData' => $processedResults]);
Then in your view, you would use JavaScript to render the chart based on the data.
While there isn't a perfect out-of-the-box solution for automatically generating graphs from raw SQL queries, these approaches can help you quickly create charts based on your database queries in Laravel.