Тому я додав цей лічильник перегляду сторінок у свій додаток, який рахує кожен візит на сторінці виконавця. Прямо зараз, якщо ви оновлюєте сторінку, лічильник оцінює +1 кожне оновлення.
Моє запитання - як я можу змусити рахувати унікальні відвідування лише по днях?
Мій контролер:
if ( $artist ) {
$getartist = $this->_artist->get($artist, "slug");
if ( $getartist ) {
$getsongs = $this->_song->collections($input, $getartist->id, "ASC");
$this->_artist->updateVisits($getartist->id);
$data = [
"is_artist" => true,
"artist" => $getartist,
"songs" => $getsongs,
"randomsongs" => $this->randomsongs,
];
return $this->view("index", $data);
}
}
Код лічильника:
$this->_artist->updateVisits($getartist->id);
Потім у моєму репозиторі виконавців:
public function updateVisits($artist_id)
{
$artist = $this->model->where("id",$artist_id)->first();
$artist->visits = $artist->visits+1;
$artist->save();
}
Тож як я запобігти підрахунку лічильника, якщо користувач оновить сторінку?
Відповіді:
0 для відповіді № 1Гаразд, я знайшов рішення навколо цього з файлами cookie Ось мій код.
public function updateVisits($artist_id)
{
$artist = $this->model->where("id",$artist_id)->first();
// Check if cookies exist
if(!isset($_COOKIE["visited_id" . $artist_id])) {
// If not set cookie and pass counter
setcookie("visited_id" . $artist_id, $artist_id, time()+30); /* expire in seconds */
$artist->visits = $artist->visits+1;
$artist->save();
// If cookie exist on user computer
} else {
//do nothing
}
}
0 для відповіді № 2
Спочатку додайте a date
поле на DB table
. Потім змініть свою функцію на наступну-
public function updateVisits($artist_id)
{
$artist = $this->model->firstOrCreate(["id" => $artist_id,"your_date_field_name"=>date("Y-m-d")]);
$artist->visits = $artist->visits+1;
$artist->save();
}
І не забудьте додати id, your_date_field_name
на fillable
масив бути mass
присвоєний