An open API service indexing awesome lists of open source software.

https://github.com/balajidharma/laravel-viewable

Track Model views for your Laravel projects.
https://github.com/balajidharma/laravel-viewable

Last synced: about 2 months ago
JSON representation

Track Model views for your Laravel projects.

Awesome Lists containing this project

README

        

Laravel Viewable


Track Page views for your Laravel projects.



Total Downloads
Latest Stable Version
License

## Credits
This package builds upon the work done in [cyrildewit/eloquent-viewable](https://github.com/cyrildewit/eloquent-viewable) and has been modified to suit specific needs. We are grateful to the original author and contributors for their work.

## Features
- Track page views for Laravel Eloquent models
- Configure unique views by IP, session, and authenticated users
- Bot detection and filtering
- Support for DNT (Do Not Track) header
- Configurable view counting and storage

## Table of Contents

- [Installation](#installation)
- [Configuration](#configuration-options)
- [Demo](#demo)

## Installation
- Install the package via composer
```bash
composer require balajidharma/laravel-viewable
```

- Publish the migration with
```bash
php artisan vendor:publish --provider="BalajiDharma\LaravelViewable\ViewableServiceProvider" --tag="migrations"
```

- Run the migration
```bash
php artisan migrate
```

- To Publish the config/viewable.php config file with
```bash
php artisan vendor:publish --provider="BalajiDharma\LaravelViewable\ViewableServiceProvider" --tag="config"
```

- Preparing your model
To associate views with a model, the model must implement the HasViewable trait:
```php
record();

```

# Laravel Viewable Configuration

This document describes all configuration options available in the `viewable.php` config file.

## Configuration Options

### Models

```php
'models' => [
'viewable' => BalajiDharma\LaravelViewable\Models\Viewable::class,
],
```

Defines the model class used for tracking views. You can override this with your own model class if needed.

```php
'table_names' => [
'viewable' => 'views',
],
```
Specifies the database table name used for storing views. Default is 'views'.

### Bot Detection

```php
'ignore_bots' => true,
```

- `true`: Ignores views from bots and crawlers
- `false`: Records views from all visitors including bots
- Default: `true`

### Do Not Track (DNT) Header

```php
'honor_dnt' => false,
```
- `true`: Respects the Do Not Track (DNT) header from browsers
- `false`: Records views regardless of DNT header
- Default: `false`

### Unique View Settings

```php
'unique_ip' => true,
'unique_session' => true,
'unique_viewer' => true,
```
Controls how unique views are tracked:

- `unique_ip`: Records only one view per IP address
- `unique_session`: Records only one view per session
- `unique_viewer`: Records only one view per authenticated user
- Default: All set to `true`

### Model View Counter

```php
'increment_model_view_count' => false,
'increment_model_column_name' => 'view_count',
```
- `increment_model_view_count`: Enable/disable automatic view count increment on the model
- `increment_model_column_name`: Specifies the column name for storing view count
- Default: Counter disabled, column name set to 'view_count'

### IP Address Filtering
```php
'ignored_ip_addresses' => [
//'127.0.0.1',
],
```
- Array of IP addresses to ignore when recording views
- Views from these IPs will not be recorded
- Default: Empty array (no IPs ignored)

## Control configuration on model

You able to control all the configurtion on model, by adding below properties

```php