Files
landing-api/app/Filament/Support/ViewsColumn.php
Josep Piguillem 3897113313 Initial commit: Pireon Admin — headless admin panel and public API
Laravel 13 + Filament 5 admin panel and REST API for a non-profit developer
collective, backed by the Pireon OAuth identity server. The public site is a
separate, server-rendered application consuming /api/public/v1.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-19 17:55:27 +02:00

47 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Filament\Support;
use Filament\Tables\Columns\TextColumn;
use Illuminate\Database\Eloquent\Model;
/**
* How often a record was read lately, in a listing.
*
* Reads the sum the query already carries — `withRecentViews()` on the
* table's own query — rather than counting per row, because a column that
* costs a query per row is a column that makes the whole list slow enough to
* stop being opened.
*
* A published record with no views at all reads differently from one with
* none because nothing has been reported yet, and the column cannot tell the
* two apart on its own. So it says nothing rather than "0": until the frontend
* starts reporting, an empty column is the honest answer.
*/
class ViewsColumn extends TextColumn
{
protected function setUp(): void
{
parent::setUp();
$this->badge();
$this->placeholder('—');
$this->alignEnd();
$this->state(fn (Model $record): ?int => $record->recent_views > 0
? (int) $record->recent_views
: null);
$this->color(fn (?int $state) => match (true) {
$state === null => 'gray',
$state >= 100 => 'success',
default => 'info',
});
$this->sortable(query: fn ($query, string $direction) => $query
->orderBy('recent_views', $direction));
}
}