Log Search Queries and Display Log Table

Filament 4
Also available in Filament 3 version

For larger applications, tracking user search behavior becomes important. This includes monitoring Global Search and Per Resource Search activities.

01JCJDDHVJ8DNXXNX79SDT4MY6

Get the Source Code:

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:

First, we will start with Resource-based Trait to modify the column search on our table:

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