PHP code example of cyrildewit / laravel-page-view-counter
1. Go to this page and download the library: Download cyrildewit/laravel-page-view-counter library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cyrildewit / laravel-page-view-counter example snippets
// 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();
use Illuminate\Database\Eloquent\Model;
use CyrildeWit\EloquentViewable\InteractsWithViews;
use CyrildeWit\EloquentViewable\Contracts\Viewable;
class Post extends Model implements Viewable
{
use InteractsWithViews;
// ...
}
views($post)->record();
// PostController.php
public function show(Post $post)
{
views($post)->record();
return view('post.show', compact('post'));
}
use CyrildeWit\EloquentViewable\Support\Period;
// Example: get views count from 2017 upto 2018
views($post)
->period(Period::create('2017', '2018'))
->count();
// Cache for 3600 seconds
views($post)->remember(3600)->count();
// Cache until the defined DateTime
views($post)->remember(now()->addWeeks(2))->count();
// Cache forever
views($post)->remember()->count();