route('forum.index'); } /** * Show the form for creating a new resource. */ public function create(Request $request) { $thread_id = $request->query('thread_id'); if (!$thread_id) { return redirect()->route('forum.index') ->with('error', 'Thread ID is required to create a topic.'); } $thread = ForumThread::findOrFail($thread_id); // Check if thread is locked if ($thread->is_locked) { return redirect()->route('forum.threads.show', $thread) ->with('error', 'This thread is locked. You cannot create new topics.'); } return view('forum.topics.create', compact('thread')); } /** * Store a newly created resource in storage. */ public function store(Request $request) { $validated = $request->validate([ 'title' => 'required|string|max:255', 'content' => 'required|string', 'thread_id' => 'required|exists:forum_threads,id', ]); $thread = ForumThread::findOrFail($validated['thread_id']); // Check if thread is locked if ($thread->is_locked) { return redirect()->route('forum.threads.show', $thread) ->with('error', 'This thread is locked. You cannot create new topics.'); } // Create the topic $topic = new ForumTopic(); $topic->thread_id = $validated['thread_id']; $topic->user_id = auth()->id(); $topic->title = $validated['title']; $topic->content = $validated['content']; $topic->last_activity_at = now(); $topic->save(); // Update thread's last activity $thread->last_activity_at = now(); $thread->save(); return redirect()->route('forum.topics.show', $topic) ->with('success', 'Topic created successfully.'); } /** * Display the specified resource. */ public function show(ForumTopic $topic) { $thread = $topic->thread; // Get posts with pagination $posts = $topic->posts() ->with('user') ->orderBy('created_at', 'asc') ->paginate(15); return view('forum.topics.show', compact('topic', 'thread', 'posts')); } /** * Show the form for editing the specified resource. */ public function edit(ForumTopic $topic) { // Check if user is authorized to edit this topic if (auth()->id() !== $topic->user_id && !auth()->user()->hasRole('admin')) { return redirect()->route('forum.topics.show', $topic) ->with('error', 'You are not authorized to edit this topic.'); } $thread = $topic->thread; // Check if thread is locked if ($thread->is_locked) { return redirect()->route('forum.topics.show', $topic) ->with('error', 'This thread is locked. You cannot edit topics.'); } return view('forum.topics.edit', compact('topic', 'thread')); } /** * Update the specified resource in storage. */ public function update(Request $request, ForumTopic $topic) { // Check if user is authorized to edit this topic if (auth()->id() !== $topic->user_id && !auth()->user()->hasRole('admin')) { return redirect()->route('forum.topics.show', $topic) ->with('error', 'You are not authorized to edit this topic.'); } $thread = $topic->thread; // Check if thread is locked if ($thread->is_locked) { return redirect()->route('forum.topics.show', $topic) ->with('error', 'This thread is locked. You cannot edit topics.'); } $validated = $request->validate([ 'title' => 'required|string|max:255', 'content' => 'required|string', ]); // Update the topic $topic->title = $validated['title']; $topic->content = $validated['content']; $topic->save(); return redirect()->route('forum.topics.show', $topic) ->with('success', 'Topic updated successfully.'); } /** * Remove the specified resource from storage. */ public function destroy(ForumTopic $topic) { // Check if user is authorized to delete this topic if (auth()->id() !== $topic->user_id && !auth()->user()->hasRole('admin')) { return redirect()->route('forum.topics.show', $topic) ->with('error', 'You are not authorized to delete this topic.'); } $thread = $topic->thread; // Check if thread is locked if ($thread->is_locked) { return redirect()->route('forum.topics.show', $topic) ->with('error', 'This thread is locked. You cannot delete topics.'); } // Check if this is the only topic in the thread if ($thread->topics()->count() <= 1) { return redirect()->route('forum.topics.show', $topic) ->with('error', 'Cannot delete the only topic in a thread. Delete the thread instead.'); } $topic->delete(); return redirect()->route('forum.threads.show', $thread) ->with('success', 'Topic deleted successfully.'); } }