Installation:
composer require awcodes/recently
php artisan recently:install
Follow the prompts to configure the package (e.g., model to track, panel integration).
First Use Case:
Add the RecentlyViewed widget to a Filament panel dashboard:
use Awcodes\Recently\Widgets\RecentlyViewed;
public function getWidgets(): array
{
return [
RecentlyViewed::make(),
];
}
This instantly displays recently viewed records for the authenticated user.
Where to Look First:
config/recently.php for tracking limits, models, and panel settings.app/Http/Middleware/TrackRecentlyViewed.php is registered in app/Http/Kernel.php.resources/views/vendor/recently/....Tracking Records:
php artisan vendor:publish --tag="recently-middleware"
use Awcodes\Recently\Facades\Recently;
Recently::track($model); // e.g., after a create/update action
Widget Integration:
RecentlyViewed::make()
->limit(5) // Override config limit
->query(fn ($query) => $query->where('active', true)) // Custom query
RecentlyViewed::make()
->forPanel('admin') // Restrict to a specific panel
->heading('Admin Recent Activity')
Data Access:
$recent = Recently::get();
$recent->take(3); // Paginate or limit results
$userRecent = Recently::forUser(auth()->user())->get();
Model-Specific Tracking:
config/recently.php:
'models' => [
App\Models\Post::class,
App\Models\User::class,
],
Recently::trackModel(App\Models\Category::class);
RecentlyViewed::make()->cacheFor(60); // Cache for 60 seconds
RecentlyViewed::make()->authorizeResourcePolicy();
Middleware Registration:
TrackRecentlyViewed middleware to Kernel.php will break automatic tracking.$middlewareGroups['web'] array.Model Tracking:
Illuminate\Database\Eloquent\Model and is whitelisted in config/recently.php.fillable includes the viewed_at timestamp if using custom tracking logic.CSS Conflicts:
php artisan vendor:publish --tag="recently-views" to publish assets and include the CSS in your theme.Performance:
viewed_at column and limit the number of tracked records in config/recently.php:
'limit' => 20,
Log Tracking Events:
Enable debug mode in config/recently.php:
'debug' => env('RECENTLY_DEBUG', false),
Check logs for tracking events in storage/logs/laravel.log.
Query Inspection:
Use Laravel’s query logging to inspect the recently_viewed table queries:
DB::enableQueryLog();
$recent = Recently::get();
dd(DB::getQueryLog());
Custom Storage:
recently_viewed table by binding a custom repository:
Recently::setRepository(App\Repositories\CustomRecentlyRepository::class);
Event Listeners:
Recently::tracked(function ($model) {
// Custom logic after a model is tracked
});
Widget Customization:
php artisan vendor:publish --tag="recently-views"
resources/views/vendor/recently/widgets/recently-viewed.blade.php.Panel-Specific Config:
getWidgets() context:
public function getWidgets(): array
{
return match ($this->getPanel()?->getId()) {
'admin' => [RecentlyViewed::make()->limit(10)],
default => [RecentlyViewed::make()->limit(5)],
};
}
Timezone Handling:
The viewed_at timestamp uses the server’s timezone. Ensure consistency by setting config/app.php:
'timezone' => 'UTC',
Soft Deletes: If using soft deletes, ensure the query accounts for deleted records:
RecentlyViewed::make()->query(fn ($query) => $query->withTrashed()),
How can I help you explore Laravel packages today?