Filament: Raw SQL Usage in Tables and Widgets

Filament 4
Also available in Filament 3 version

Five examples of raw SQL usage with MySQL database.

01JE3BT9B01JHCQ8CXM5HW5F42

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'),
DB::raw('ROUND(AVG(`load`), 2) as `load`'),
DB::raw('ROUND(AVG(memory_total), 2) as memory_total'),
DB::raw('ROUND(AVG(memory_used), 2) as memory_used'),
DB::raw('ROUND(AVG(memory_free), 2) as memory_free'),
DB::raw('ROUND(AVG(disk_total), 2) as disk_total'),
DB::raw('ROUND(AVG(disk_used), 2) as disk_used'),
DB::raw('ROUND(AVG(disk_free), 2) as disk_free'),
],
)
->groupByRaw('server_id');
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 152 Premium Examples for $99