2
みなさん、こんにちは、私はポストの詳細を表示する機能のショーを()は、ここに私のコードです:Laravelで人気のポストを作成する方法5.4
public function show(Post $post)
{
//This line code was used to increment post when someone hit the post
$viewCount = $post->view_count + 1;
$post->update(['view_count' => $viewCount]);
$next_id = Post::where('id', '>', $post->id)->min('id');
$prev_id = Post::where('id', '<', $post->id)->max('id');
return view('site.show', compact('post'))
->with('next', Post::find($next_id))
->with('prev', Post::find($prev_id));
}
誰かがページを更新するか、打ったときの問題があり、それは何度も何度も繰り返すでしょう。私の質問は、セッションまたはIPアドレスを使用してこの問題を解決して、同じユーザーを防ぐ方法です。私の英語文法よくない場合は申し訳ありませんが、おかげで事前
に私がlaracastsからこれを得たが、私は私のコードでそれを実装する方法がわからない。ここで
$blogKey = 'blog_' . $id;
// Check if blog session key exists
// If not, update view_count and create session key
if (!Session::has($blogKey)) {
Post::where('id', $id)->increment('view_count');
Session::put($blogkey, 1);
}
は私が変更された最終的なコードです:
public function show(Post $post)
{
$siteKey = $post->id;
// Check if site session key exists
if (!Session::has($siteKey)) {
// If not, update view_count and create session key
Post::where('id', $post->id)->increment('view_count');
//Set the session key so we don't increment for the session duration
Session::put($siteKey, 1);
}
$next_id = Post::where('id', '>', $post->id)->min('id');
$prev_id = Post::where('id', '<', $post->id)->max('id');
return view('site.show', compact('post'))
->with('next', Post::find($next_id))
->with('prev', Post::find($prev_id));
}
あなたのコードの最初の4行を削除し、代わりにlaracastsコードを使用すると、それは –
@DhavalChhedaがあなたの提案をありがとうございました、それが動作するようになりまし動作するかどうかを確認:) –