Filament: Raw SQL Usage in Tables and Widgets

While most data can be accessed via a simple select query, Raw SQL is sometimes required. For example, if we want to take averages of multiple columns.

FilamentExamples ThumbBase - 2024-12-02T113220.812

Get the Source Code:

How it works

Below are five raw SQL with MySQL syntax examples, along with the location where you can find them so you can explore more.

1. AVG() with ROUND()

app/Filament/Resources/MetricResource.php:

return $table
->modifyQueryUsing(function (Builder $query) {
$query->select(
[
'id',
'server_id',
DB::raw('created_at as date'),
// ...

Example queries in this project:

  • avg() and round() usage
  • if() usage
  • case ... when ... usage
  • date_format() usage
  • concat() usage

All examples will include real use-case in the code. From tables to dashboard widgets.

The FULL tutorial is available after the purchase: in the Readme file of the official repository you would get invited to.
Get the Source Code: All 68 Premium Examples for $99