### Composer Package Installation Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/UPGRADING.md Command to install the new `cyrildewit/eloquent-viewable` composer package after a rename from `Laravel Page View Counter`. ```bash composer require cyrildewit/eloquent-viewable ``` -------------------------------- ### Installation and Setup Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/README.md Steps to install the Eloquent Viewable package using Composer, publish migrations, run database migrations, and optionally publish the configuration file. ```winbatch composer require cyrildewit/eloquent-viewable ``` ```winbatch php artisan vendor:publish --provider="CyrildeWit\EloquentViewable\EloquentViewableServiceProvider" --tag="migrations" ``` ```winbatch php artisan migrate ``` ```winbatch php artisan vendor:publish --provider="CyrildeWit\EloquentViewable\EloquentViewableServiceProvider" --tag="config" ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/CONTRIBUTING.md Installs the necessary package dependencies for the project using Composer. This is a prerequisite for running tests and other development tasks. ```winbatch composer install ``` -------------------------------- ### Eloquent Viewable Usage Examples (PHP) Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/README.md Demonstrates common ways to use the Eloquent Viewable package to track and retrieve view counts for Eloquent models. It covers retrieving total and unique views, filtering by date periods, recording new views, and applying cooldowns. This functionality requires the Eloquent Viewable package installed in a Laravel application. ```PHP // Return total views count views($post)->count(); // Return total views count that have been made since 20 February 2017 views($post)->period(Period::since('2017-02-20'))->count(); // Return total views count that have been made between 2014 and 2016 views($post)->period(Period::create('2014', '2016'))->count(); // Return total unique views count (based on visitor cookie) views($post)->unique()->count(); // Record a view views($post)->record(); // Record a view with a cooldown views($post)->cooldown(now()->addHours(2))->record(); ``` -------------------------------- ### Publish Configuration File Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/UPGRADING.md Publishes the configuration file for the Eloquent Viewable package. After publishing, users should review and update the configuration settings as needed. ```winbatch php artisan vendor:publish --provider="CyrildeWit\EloquentViewable\EloquentViewableServiceProvider" --tag="config" ``` -------------------------------- ### Publish Migration File Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/UPGRADING.md Publishes the migration file for updating database tables. This command is used during development to make the new migration available. ```winbatch php artisan vendor:publish --provider="CyrildeWit\EloquentViewable\EloquentViewableServiceProvider" --tag="migrations" ``` -------------------------------- ### Update Session Configuration Snippet Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/UPGRADING.md Introduces a new session configuration snippet for the package, specifying the key under which session data is stored. ```php 'session' => [ /* * Everthing will be stored under the following key. */ 'key' => 'cyrildewit.eloquent-viewable.session', ] ``` -------------------------------- ### Run PHPUnit Tests Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/CONTRIBUTING.md Executes the project's test suite using PHPUnit. Ensure Composer dependencies are installed before running this command. ```winbatch vendor/bin/phpunit ``` -------------------------------- ### Update Service Provider in config/app.php Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/UPGRADING.md Instruction to update the service provider class in `config/app.php` when the package is renamed. ```php CyrildeWit\EloquentViewable\EloquentViewableServiceProvider::class ``` -------------------------------- ### Remove Cache Lifetime Configuration Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/UPGRADING.md When upgrading to v6.0.0, if you have published the package's configuration file, you need to manually remove the 'cache.lifetime_in_minutes' key. ```diff -/* - * Default lifetime of cached views count in minutes. - */ -'lifetime_in_minutes' => 60, ``` -------------------------------- ### Update Visitor Contract Methods Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/UPGRADING.md The `ip` method within the `Visitor` contract has had its return type hint updated in v6.0.0 to `?string`, indicating it may now return null. ```php /** * The `ip` method has now `?string` as return typehint. */ ``` -------------------------------- ### Update Config File Key Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/UPGRADING.md Changes the configuration key for session cooldown from `session` to `cooldown`. If the config file was published, this key needs to be updated. ```php 'cooldown' => [ /* * Everthing will be stored under the following key in the session. */ 'key' => 'cyrildewit.eloquent-viewable.cooldowns', ] ``` -------------------------------- ### Update Views Contract Methods Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/UPGRADING.md Several methods within the `Views` contract have had their parameter type hints or return type hints updated in v6.0.0, requiring adjustments if you are extending or directly using these methods. ```php /** * The `$viewable` argument of the `forViewable` method cannot be `null` anymore. * The `record` method has now `bool` as return typehint. * The `destroy` method has now `void` as return typehint. * The `$period` argument of the `period` method has now a typehint of `?Period`. * The `$name` argument of the `period` method has now a typehint of `?string`. * The default value `null` of the `$lifetime` argument of the `remember` method has been removed. */ ``` -------------------------------- ### Update Visitor Column Type Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/UPGRADING.md Changes the data type of the `visitor` column in the database to `text`. This requires a migration to alter the existing column. ```php $table->text('visitor')->change(); ``` -------------------------------- ### Update Eloquent Model Trait Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/UPGRADING.md Renames the `CyrildeWit\EloquentViewable\Viewable` trait to `CyrildeWit\EloquentViewable\InteractsWithViews`. This change requires updating model definitions that use this trait. ```php use CyrildeWit\EloquentViewable\InteractsWithViews; ``` -------------------------------- ### Update Viewable Model Trait Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/UPGRADING.md Demonstrates how to update the model trait usage. It involves replacing the old `HasPageViewCounter` trait with the new `Viewable` trait. ```php use Illuminate\Database\Eloquent\Model; use CyrildeWit\EloquentViewable\Viewable; class Post extends Model { use Viewable; // ... } ``` -------------------------------- ### Update orderByUniqueViews Query Scope Signature Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/UPGRADING.md The signature for the `orderByUniqueViews` query scope in the `Viewable` contract has been updated in v6.0.0 to include new parameters for more flexible querying. ```diff -public function scopeOrderByUniqueViews(Builder $query, string $direction = 'desc', $period = null): Builder; +public function scopeOrderByUniqueViews(Builder $query, string $direction = 'desc', ?Period $period = null, ?string $collection = null, string $as = 'unique_views_count'): Builder; ``` -------------------------------- ### Handle Null Viewable in Views Helper Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/UPGRADING.md In v5.1.0, using the `views()` helper with a null viewable argument, such as `views()->count()`, is no longer valid. You should now use the `View` Eloquent model directly. ```php /** * The following code is not valid anymore: * views()->count(); * * Use the `View` Eloquent model. */ ``` -------------------------------- ### Update orderByViews Query Scope Signature Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/UPGRADING.md The signature for the `orderByViews` query scope in the `Viewable` contract has been updated in v6.0.0 to include new parameters for more flexible querying. ```diff -public function scopeOrderByViews(Builder $query, string $direction = 'desc', $period = null): Builder; +public function scopeOrderByViews(Builder $query, string $direction = 'desc', ?Period $period = null, ?string $collection = null, bool $unique = false, string $as = 'views_count'): Builder; ``` -------------------------------- ### Viewable Model Method Updates Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/UPGRADING.md Details the renaming of methods for tracking page views. This includes changes from `addPageView` to `addView`, and various `getPageViews` and `getUniquePageViews` methods to `getViews` and `getUniqueViews` respectively, with updated parameter handling for date ranges. ```APIDOC Method Renames: - `->addPageView()` is now `->addView()` - `->addPageViewThatExpiresAt()` is now `->addView()` - `->getPageViews()` (e.g., `getPageViews()`, `getPageViewsFrom()`, `getPageViewsBefore()`, `getPageViewsBetween()`) are now `->getViews()` with optional `Period` objects. - `getPageViews()` -> `getViews()` - `getPageViewsFrom()` -> `getViews(Period::since())` - `getPageViewsBefore()` -> `getViews(Period::upto())` - `getPageViewsBetween()` -> `getViews(Period::create(, ))` - `->getUniquePageViews()` is now `->getUniqueViews()` - `->getUniquePageViewsFrom()` is now `->getUniqueViews(Period::since())` - `->getUniquePageViewsBefore()` is now `->getUniqueViews(Period::upto())` - `->getUniquePageViewsBetween()` is now `->getUniqueViews(Period::create(, ))` Note: The `addPageViewThatExpiresAt` functionality is available again in v2.1.0. ``` -------------------------------- ### Update Session Delay Method Name Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/UPGRADING.md Renames the `delayInSession` method on the `Views` builder to `cooldown`. This affects how session-based view cooldowns are configured. ```php views($post)->cooldown(now()->addMinutes(30))->record(); ``` -------------------------------- ### Update Views Database Table Primary Key Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/UPGRADING.md Modifies the `id` column in the `views` database table to be a big integer. This requires creating a new migration to alter the table schema. ```php $table->bigIncrements('id')->change(); ``` -------------------------------- ### Add Custom Macro to Views Class Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/README.md Extend the Views builder class by adding custom macros. This example demonstrates adding a 'countAndRemember' macro for convenience in counting and caching view results. ```php use CyrildeWit\EloquentViewable\Views; Views::macro('countAndRemember', function () { return $this->remember()->count(); }); // Usage: views($post)->countAndRemember(); Views::forViewable($post)->countAndRemember(); ``` -------------------------------- ### Get View Count of a Viewable Type Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/README.md Provides methods to retrieve the total view count for a specific viewable Eloquent model type. You can pass an instance of the model or its class name to the `views()` helper function, and then call the `count()` method. ```php views(new Post())->count(); views(Post::class)->count(); views('App\Post')->count(); ``` -------------------------------- ### Recording Views Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/README.md Demonstrates how to record a view for a model instance using the views() helper and record() method, typically within a controller. ```php views($post)->record(); ``` ```php // PostController.php public function show(Post $post) { views($post)->record(); return view('post.show', compact('post')); } ``` -------------------------------- ### Model Preparation Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/README.md How to prepare an Eloquent model to be viewable by implementing the Viewable interface and using the InteractsWithViews trait. ```php use Illuminate\Database\Eloquent\Model; use CyrildeWit\EloquentViewable\InteractsWithViews; use CyrildeWit\EloquentViewable\Contracts\Viewable; class Post extends Model implements Viewable { use InteractsWithViews; // ... } ``` -------------------------------- ### Manual Service Provider Registration Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/README.md Instructions for manually registering the Eloquent Viewable service provider in the application's config/app.php file. ```php // config/app.php 'providers' => [ // ... CyrildeWit\EloquentViewable\EloquentViewableServiceProvider::class, ]; ``` -------------------------------- ### Setting View Cooldown Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/README.md Explains how to set a cooldown period for view recording using the cooldown() method, accepting minutes or a DateTimeInterface instance. ```php views($post) ->cooldown($minutes) ->record(); ``` ```php $expiresAt = now()->addHours(3); views($post) ->cooldown($expiresAt) ->record(); ``` -------------------------------- ### Git Commit Message Format Source: https://github.com/cyrildewit/eloquent-viewable/blob/main/CONTRIBUTING.md Specifies the required format for Git commit messages, including type, scope, subject, body, and footer. Adheres to a 100-character line limit for improved readability across different tools. ```apidoc ():