PostsController.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Post;
  4. use Illuminate\Http\Request;
  5. class PostsController extends Controller
  6. {
  7. /**
  8. * Show the form for creating a new resource.
  9. *
  10. * @return \Illuminate\Http\Response
  11. */
  12. public function create()
  13. {
  14. return view('posts.create');
  15. }
  16. /**
  17. * Remove the specified resource from storage.
  18. *
  19. * @param int $id
  20. *
  21. * @return \Illuminate\Http\Response
  22. */
  23. public function destroy($id)
  24. {
  25. //
  26. }
  27. /**
  28. * Show the form for editing the specified resource.
  29. *
  30. * @param int $id
  31. *
  32. * @return \Illuminate\Http\Response
  33. */
  34. public function edit($id)
  35. {
  36. //
  37. }
  38. /**
  39. * Display a listing of the resource.
  40. *
  41. * @return \Illuminate\Http\Response
  42. */
  43. public function index()
  44. {
  45. // $posts = Post::orderBy('created_at', 'desc')->get();
  46. $posts = Post::latest()->get();
  47. return view('posts.index', compact('posts'));
  48. }
  49. public function show(Post $post)
  50. {
  51. return view('posts.show', compact('post'));
  52. }
  53. /**
  54. * Store a newly created resource in storage.
  55. *
  56. * - Create new post using the request data
  57. * - Save it to the database
  58. * - Redirect to the home page
  59. *
  60. * @param \Illuminate\Http\Request $request
  61. *
  62. * @return \Illuminate\Http\Response
  63. */
  64. public function store(Request $request)
  65. {
  66. // $post = new Post();
  67. // $post->title = $request->get('title');
  68. // $post->body = $request->get('body');
  69. // $post->save();
  70. $this->validate($request, [
  71. 'title' => 'required|min:10',
  72. 'body' => 'required',
  73. ]);
  74. Post::create([
  75. 'title' => $request->get('title'),
  76. 'body' => $request->get('body'),
  77. ]);
  78. return redirect('/');
  79. }
  80. /**
  81. * Update the specified resource in storage.
  82. *
  83. * @param \Illuminate\Http\Request $request
  84. * @param int $id
  85. *
  86. * @return \Illuminate\Http\Response
  87. */
  88. public function update(Request $request, $id)
  89. {
  90. //
  91. }
  92. }