JD
John Doe
john@example.com
Blog App
Laravel 10.x • PHP 8.2 • MySQL
Files
app
Http
Controllers
PostController.php
CommentController.php
UserController.php
Middleware
Models
database
resources
1<?php23namespace App\Http\Controllers;45use App\Models\Post;6use Illuminate\Http\Request;7use Illuminate\Support\Facades\Auth;89class PostController extends Controller10{11 /**12 * Display a listing of the resource.13 */14 public function index()15 {16 $posts = Post::with('user', 'comments')17 ->latest()18 ->paginate(10);1920 return view('posts.index', compact('posts'));21 }2223 /**24 * Show the form for creating a new resource.25 */26 public function create()27 {28 return view('posts.create');29 }3031 /**32 * Store a newly created resource in storage.33 */34 public function store(Request $request)35 {36 $validated = $request->validate([37 'title' => 'required|max:255',38 'content' => 'required',39 'published' => 'boolean'40 ]);4142 $post = Auth::user()->posts()->create($validated);4344 return redirect()->route('posts.show', $post)45 ->with('success', 'Post created successfully!');46 }4748 /**49 * Display the specified resource.50 */51 public function show(Post $post)52 {53 return view('posts.show', compact('post'));54 }5556 /**57 * Show the form for editing the specified resource.58 */59 public function edit(Post $post)60 {61 $this->authorize('update', $post);6263 return view('posts.edit', compact('post'));64 }6566 /**67 * Update the specified resource in storage.68 */69 public function update(Request $request, Post $post)70 {71 $this->authorize('update', $post);7273 $validated = $request->validate([74 'title' => 'required|max:255',75 'content' => 'required',76 'published' => 'boolean'77 ]);7879 $post->update($validated);8081 return redirect()->route('posts.show', $post)82 ->with('success', 'Post updated successfully!');83 }8485 /**86 * Remove the specified resource from storage.87 */88 public function destroy(Post $post)89 {90 $this->authorize('delete', $post);9192 $post->delete();9394 return redirect()->route('posts.index')95 ->with('success', 'Post deleted successfully!');96 }97}
Try: "Generate a migration for users table" or "Optimize this query"