/ / Laravel 4.2ページビューカウンター-laravel、laravel-4

Laravel 4.2ページビューカウンター-laravel、laravel-4

そこで、アーティストページでの各訪問をカウントするこのページビューカウンターをアプリに追加しました。現在、ページを更新すると、カウンターは更新ごとに+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);

その後、私のArtistRepositoryで:

public function updateVisits($artist_id)
{
$artist = $this->model->where("id",$artist_id)->first();
$artist->visits = $artist->visits+1;
$artist->save();
}

ユーザーがページを更新した場合、カウンターがカウントされないようにするにはどうすればよいですか?

回答:

回答№1は0

わかりました、私はこの問題をクッキーで解決しました ここに私のコードがあります。

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
}
}

回答№2の場合は0

まず追加する 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_namefillable 配列 mass 割り当て可能。