###前提・実現したいこと
Laravel5.4にてユーザの詳細画面を表示する回数に制限をかけたいと思っています。
DBにカウント用のカラムを追加し、詳細画面を表示できたらインクリメントするようにしています。
規定回数(ここでは10回)を超えた場合にエラーメッセージと共にトップページへリダイレクトさせたいのですが
エラーメッセージのみ表示され、トップページへ遷移しませんでした。
[DB情報] table名 users カラム名 user_details_counts
###該当のソースコード
PHP
1UserController.php 2 3private function CountLimit($request) 4 { 5 $counts = DB::table('users') 6 ->where('id', Auth::user()->id) 7 ->value('user_details_counts'); 8 9 if (response($request, 200)){ 10 try { 11 DB::table('users') 12 ->where('id', Auth::user()->id) 13 ->increment('user_details_counts'); 14 if ($counts >= 10){ 15 throw new \Exception(); 16 } 17 } 18 catch (\Exception $e){ 19 Log::error($e->getMessage()); 20 //topページへリダイレクトされない 21 return redirect('/') 22 ->withErrors('ページを表示できません。'); 23 } 24 } 25 return $request; 26 } 27 28public function userDetails(Request $request) 29 { 30 $this->CountLimit($request); 31 //その他の処理(省略) 32 33 return view('user.details'); 34 }
ご指摘、ご教授よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/04 07:02