블로그 Post가 클릭되면 pageview를 하나 증가시키는데, 이때 timestamps(updated_at 컬럼)도 같이 갱신되는 문제가 있었다.
로그를 보면 pageview뿐만 아니라 updated_at 컬럼도 갱신되는 것을 볼 수 있다.
$post = Post::findOrFail($id);
$post->increment('pageview');
[2017-02-09 15:49:32] local.INFO: sql => update `posts` set `pageview` = `pageview` + 1, `updated_at` = ? where `id` = ?
[2017-02-09 15:49:32] local.INFO: array (
0 => '2017-02-09 15:49:32',
1 => 2,
)
[2017-02-09 15:49:32] local.INFO: elapsed time => 0.37
timestamps를 touch하지 않고 update하려면 아래와 같이 timestamps 속성값을 false로 해주면 된다.
그러면 updated_at 컬럼은 갱신되지 않고 pageview 컬럼만 갱신된다.
$post = Post::findOrFail($id);
$post->timestamps = false;
$post->increment('pageview');
[2017-02-09 15:48:40] local.INFO: sql => update `posts` set `pageview` = `pageview` + 1 where `id` = ?
[2017-02-09 15:48:40] local.INFO: array (
0 => 13,
)
[2017-02-09 15:48:40] local.INFO: elapsed time => 0.77