File tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed
app/Http/Controllers/Forum Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -91,6 +91,25 @@ public function create(): View
9191
9292 public function store (ThreadRequest $ request ): RedirectResponse
9393 {
94+ $ userId = Auth::id (); // Get the authenticated user's ID
95+ $ midnight = now ()->endOfDay ();
96+ $ remainingSeconds = $ midnight ->diffInSeconds (now ());
97+
98+ // Count the threads posted by the user today
99+ $ cacheKey = "user_threads_count_ {$ userId }" ;
100+ $ threadCount = Cache::remember ($ cacheKey , $ remainingSeconds , function () use ($ userId ) {
101+ return Thread::where ('author_id ' , $ userId )
102+ ->where ('created_at ' , '>= ' , now ()->startOfDay ())
103+ ->count ();
104+ });
105+
106+ // Check if the user has reached the limit
107+ if ($ threadCount >= 1 ) {
108+ $ this ->error ('You can only post a maximum of 5 threads per day. ' );
109+ return redirect ()->route ('forum ' );
110+ }
111+
112+
94113 $ this ->dispatchSync (CreateThread::fromRequest ($ request , $ uuid = Str::uuid ()));
95114
96115 $ thread = Thread::findByUuidOrFail ($ uuid );
You can’t perform that action at this time.
0 commit comments