2017-12-31 39 views
0

그래서이 페이지 뷰 카운터를 아티스트 페이지의 각 방문을 계산하는 앱에 추가했습니다. 지금 당장 페이지를 새로 고치면 카운터가 새로 고침됩니다.Laravel 4.2 페이지 뷰 카운터

내 질문은 어떻게 고유 한 방문을 하루에 계산할 수 있습니까?

내 컨트롤러 :

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

그래서 방법은 내가 계산에서 카운터 방지 않으면 사용자가 새로 고침 그 페이지?

답변

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

을 다음과 같은 - 당신의 기능을 변경 그리고 mass 할당 할 fillable 배열에 id, your_date_field_name를 추가해야합니다.

+0

"대량 할당 가능하도록 채우기 가능한 배열에 your_date_field_name이라는 ID를 추가하는 것을 잊지 마십시오." 나는 이것을 이해하지 못했다. 나는 당신의 솔루션을 그대로 테스트하고 매번 db마다 새로운 빈 필드를 생성합니다. 순 방문자 수를 계산하는 방법은 무엇입니까? 나는 그것이 어떻게 다시 설정되는지 그리고 그것이 새로운 방문객을 어떻게 알았 는가? – Angelos

+0

웅변 모델 클래스에는 채울 수있는 배열이 있습니다. id, another_field를 추가하십시오. – Sohel0415

+0

보호 된 $ 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 
} 
}