PostsController.php 2.5 KB

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