1. Go to this page and download the library: Download davidjr82/eloquent-viewable 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/ */
davidjr82 / eloquent-viewable example snippets
// Get the total number of views
$post->getViews();
// Get the total number of views since the given date
$post->getViews(Period::since(Carbon::parse('2014-02-23 00:00:00')));
// Get the total number of views between the given date range
$post->getViews(Period::create(Carbon::parse('2014-00-00 00:00:00'), Carbon::parse('2016-00-00 00:00:00')));
// Get the total number of views in the past 6 weeks (from today)
$post->getViews(Period::pastWeeks(6));
// Get the total number of views in the past 2 hours (from now)
$post->getViews(Period::subHours(2));
// Store a new view in the database
$post->addView();
/**
* Get the total number of views.
*
* @param \CyrildeWit\EloquentViewable\Support\Period
* @return int
*/
public function getViews($period = null): int;
/**
* Get the total number of unique views.
*
* @param \CyrildeWit\EloquentViewable\Support\Period
* @return int
*/
public function getUniqueViews($period = null) : int;
// Create a new Period instance.
Period::create(DateTime $startDateTime = null, DateTime $endDateTime = null);
// Create a new Period instance with only a start date time.
Period::since(DateTime $startDateTime);
// Create a new Period instance with only a end date time.
Period::upto(DateTime $endDateTime);
// Period instance with a start date time of today minus the given days.
Period::pastDays(int $days);
// Period instance with a start date time of today minus the given weeks.
Period::pastWeeks(int $weeks);
// Period instance with a start date time of today minus the given months.
Period::pastMonths(int $months);
// Period instance with a start date time of today minus the given years.
Period::pastYears(int $years);
// Period instance with a start date time of now minus the given seconds.
Period::subSeconds(int $seconds);
//Period instance with a start date time of now minus the given minutes.
Period::subMinutes(int $minutes);
// Period instance with a start date time of now minus the given hours.
Period::subHours(int $hours);
// Period instance with a start date time of now minus the given days.
Period::subDays(int $days);
// Period instance with a start date time of now minus the given weeks.
Period::subWeeks(int $weeks);
// Period instance with a start date time of now minus the given months.
Period::subMonths(int $months);
// Period instance with a start date time of now minus the given years.
Period::subYears(int $years);
use CyrildeWit\EloquentViewable\ViewTracker;
// Get the total number of views of one type
ViewTracker::getViewsCountByType(Post::class);
// Get the total number of views of multiple types
ViewTracker::getViewsCountByTypes([Post::class, Location::class, Hotel::class]);
// ...
public function getViewsSince(DateTime $sinceDateTime)
{
return $this->getViews(Period::since($sinceDateTime));
}
public function getViewsUpto(DateTime $uptoDateTime)
{
return $this->getViews(Period::upto($uptoDateTime));
}
public function getViewsBetween(DateTime $sinceDateTime, DateTime $uptoDateTime)
{
return $this->getViews(Period::create($sinceDateTime, $uptoDateTime));
}
public function getViewsInPastDays(int $days)
{
return $this->getViews(Period::pastDays($days));
}
// ...