Log Search Queries and Display Log Table

Filament 3
Also available in Filament 4/5 version

If you have many searchable resources, you can log what people looked for to gain more insights into your application.

FilamentExamples ThumbBase (79)

Get the Source Code:

Only This Example

$9

One-time payment

Full source code for Log Search Queries and Display Log Table
Downloadable ZIP file with the source code
Lifetime access to this example
GitHub Sign in with GitHub to buy

Sign in first, then complete your $9 checkout.

Best value — all 168 examples

FilamentExamples Membership

$99 /year
or
$199 lifetime
Access to code of all 168 examples
Future new examples and updates included
FilaCheck Pro package licence included
MCP server included
View membership plans

30-day money-back guarantee

How it works

To log our searches, we first need a table:

Migration

Schema::create('search_logs', function (Blueprint $table) {
$table->id();
$table->string('resource');
$table->string('search_query');
$table->foreignId('user_id')->constrained('users');
$table->timestamps();
});

And a Model:

app/Models/SearchLog.php

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
 
class SearchLog extends Model
{
protected $fillable = [
'resource',
'search_query',
'user_id',
];
 
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}

Once these are in place, we can work on creating our reusable Trait to log the search queries:

Next, we will create:

  • Trait for Table logging - to prevent code repetition as much as possible
  • Trait for Global logging
  • Custom resource with Search Logs to view what users searched for
The FULL tutorial is available after the purchase: in the Readme file of the official repository you would get invited to.