Files
movies/app/Http/Controllers/Forum/TopicsController.php
T

90 lines
2.6 KiB
PHP
Raw Normal View History

2025-01-27 22:40:26 +02:00
<?php
namespace App\Http\Controllers\Forum;
use App\Http\Controllers\Controller;
2025-02-10 01:58:12 +02:00
use App\Http\Requests\Forum\TopicRequest;
2025-02-16 21:11:07 +02:00
use App\Models\Forum\Category;
use App\Models\Forum\Post;
use App\Models\Forum\Thread;
use App\Models\Forum\Topic;
2025-01-27 22:40:26 +02:00
use Illuminate\Http\Request;
2025-02-16 21:11:07 +02:00
use Illuminate\Support\Str;
2025-01-27 22:40:26 +02:00
class TopicsController extends Controller
{
2025-02-16 21:11:07 +02:00
public function show(string $categorySlug, string $threadSlug, string $topicSlug)
2025-02-10 01:58:12 +02:00
{
2025-02-16 21:11:07 +02:00
$thread = Thread::with('category')->where('slug', $threadSlug)->first();
2025-08-16 15:38:07 +03:00
$topic = Topic::with('author')->where('slug', $topicSlug)->first();
if (!$topic || !$thread) {
abort(404);
}
2025-02-16 21:11:07 +02:00
$topic->views_no = $topic->views_no+1;
$topic->save();
$posts = Post::with('author')->where('topic_id', $topic->id)->paginate(15);
2025-08-16 15:38:07 +03:00
// Get similar topics from the same thread
$similarTopics = Topic::where('thread_id', $thread->id)
->where('id', '!=', $topic->id)
->limit(5)
->get();
return view('forum.topics.show', compact('thread', 'topic', 'posts', 'similarTopics'));
2025-02-16 21:11:07 +02:00
}
public function create(string $categorySlug, string $threadSlug)
{
// check policy
$forumCategories = Category::get();
$forumThreads = Thread::get();
$currentThread = Thread::with('category')->where('slug', $threadSlug)->first();
return view('forum.topics.create', compact('forumCategories', 'forumThreads', 'currentThread'));
}
public function store(TopicRequest $request, string $categorySlug, string $threadSlug)
{
// check policy
$thread = Thread::with('category')->where('slug', $threadSlug)->first();
$topic = Topic::create([
'name' => $request->get('name'),
'slug' => Str::slug($request->get('name')),
'status' => Topic::STATUS_OPEN,
'category_id' => $thread->category->id,
'thread_id' => $thread->id,
'user_id' => auth()->user()->id,
]);
Post::create([
'content' => $request->get('content'),
'user_id' => auth()->user()->id,
'category_id' => $thread->category->id,
'thread_id' => $thread->id,
'topic_id' => $topic->id,
]);
return redirect()->route('forums.thread.topics.show', [$categorySlug, $threadSlug, $topic->slug]);
}
public function edit(string $categorySlug, string $threadSlug, string $topicSlug)
{
// check policy
}
public function update(TopicRequest $request)
{
// check policy
}
public function destroy()
{
// check policy
2025-02-10 01:58:12 +02:00
}
2025-01-27 22:40:26 +02:00
}