2017-12-31 21 views
0

私はこのページビューカウンタをアーティストページの各訪問をカウントする私のアプリに追加しました。今すぐページを更新すると、カウンターは+1ごとに更新されます。Laravel 4.2ページビューカウンタ

私の質問は、どのようにしてユニークな訪問を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); 

     } 

    } 

カウンタのコードは次のとおりです。

私ArtistRepositoryで次に
$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

最初にDB tabledateフィールドを追加します。その後

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(); 
} 

をfollowing-するとmass割り当て可能であることをid, your_date_field_namefillable上のアレイを追加することを忘れないでください。あなたの関数を変更

+0

"大量の割り当て可能なfillable配列にid、your_date_field_nameを追加することを忘れないでください。"私はこれを理解していませんでした。私はあなたのソリューションをそのままテストし、毎回私のdbに新しい空のフィールドを作成しています。一意の訪問回数もカウントする予定ですか?私はそれがどのようにリセットされ、どのように私は新しい訪問者を知っているそれを得るか? – Angelos

+0

あなたの雄弁なモデルクラスには、塗りつぶし可能な配列があります。 id、another_fieldをそこに追加します。 – Sohel0415

+0

protected $ fillable = ['id'、 'your_date_field_name']; – Sohel0415

0

私はクッキーでこれを解決しました。 Heres my code。

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