CommentController.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Comment;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\DB;
  6. class CommentController extends Controller
  7. {
  8. /**
  9. * Display a listing of the resource.
  10. *
  11. * @return \Illuminate\Http\Response
  12. */
  13. public function index()
  14. {
  15. //
  16. }
  17. public function index_count()
  18. {
  19. return Comment::select('blogId', DB::raw('count(*) as total'))->groupBy('blogId')->reorder('total', 'desc')->get();
  20. }
  21. public function index_by_blog(Request $request)
  22. {
  23. return Comment::where('blogId', '=', $request->blogId)->get();
  24. }
  25. /**
  26. * Show the form for creating a new resource.
  27. *
  28. * @return \Illuminate\Http\Response
  29. */
  30. public function create()
  31. {
  32. //
  33. }
  34. /**
  35. * Store a newly created resource in storage.
  36. *
  37. * @param \Illuminate\Http\Request $request
  38. * @return \Illuminate\Http\Response
  39. */
  40. public function store(Request $request)
  41. {
  42. Comment::insert([
  43. 'authorId' => $request->user()->id,
  44. 'comment' => $request->comment,
  45. 'blogId' => $request->blogId,
  46. 'created_at' => now(),
  47. 'updated_at' => now(),
  48. ]);
  49. }
  50. /**
  51. * Display the specified resource.
  52. *
  53. * @param \App\Models\Comment $comment
  54. * @return \Illuminate\Http\Response
  55. */
  56. public function show(Request $request)
  57. {
  58. return Comment::where('id', '=', $request->commentId)->first();
  59. }
  60. /**
  61. * Show the form for editing the specified resource.
  62. *
  63. * @param \App\Models\Comment $comment
  64. * @return \Illuminate\Http\Response
  65. */
  66. public function edit(Comment $comment)
  67. {
  68. //
  69. }
  70. /**
  71. * Update the specified resource in storage.
  72. *
  73. * @param \Illuminate\Http\Request $request
  74. * @param \App\Models\Comment $comment
  75. * @return \Illuminate\Http\Response
  76. */
  77. public function update(Request $request, Comment $comment)
  78. {
  79. //
  80. }
  81. /**
  82. * Remove the specified resource from storage.
  83. *
  84. * @param \App\Models\Comment $comment
  85. * @return \Illuminate\Http\Response
  86. */
  87. public function destroy(Request $request)
  88. {
  89. Comment::where('id', '=', $request->commentId)->delete();
  90. }
  91. }