php/app/Http/Controllers/PostController.php
m1ngsama 21ace0f38e Add authentication and community controllers
Implemented user registration/login system and community management features.
Added controllers for handling posts and community subscriptions.
2025-12-04 09:45:00 +08:00

55 lines
1.5 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Community;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function create()
{
$communities = Community::orderBy('name')->get();
return view('posts.create', compact('communities'));
}
public function store(Request $request)
{
$validated = $request->validate([
'community_id' => 'required|exists:communities,id',
'title' => 'required|string|max:255',
'content' => 'nullable|string',
'url' => 'nullable|url',
'type' => 'required|in:text,link,image',
]);
$post = Post::create([
'community_id' => $validated['community_id'],
'user_id' => auth()->id(),
'title' => $validated['title'],
'content' => $validated['content'] ?? null,
'url' => $validated['url'] ?? null,
'type' => $validated['type'],
]);
return redirect()->route('posts.show', $post)
->with('success', 'Post created successfully!');
}
public function show(Post $post)
{
$post->load(['user', 'community', 'comments.user', 'comments.replies.user']);
return view('posts.show', compact('post'));
}
public function destroy(Post $post)
{
if ($post->user_id !== auth()->id()) {
abort(403);
}
$post->delete();
return redirect('/')->with('success', 'Post deleted successfully!');
}
}