Advertisement
hprihutomo

Controller Comment

Nov 20th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use App\Comment;
  6. use Illuminate\Http\Request;
  7.  
  8. class CommentController extends Controller
  9. {
  10. /**
  11. * Display a listing of the resource.
  12. *
  13. * @return \Illuminate\Http\Response
  14. */
  15. public function index()
  16. {
  17. $comments=Comment::all();
  18. return response()->json($comments,200);
  19. }
  20.  
  21. /**
  22. * Show the form for creating a new resource.
  23. *
  24. * @return \Illuminate\Http\Response
  25. */
  26.  
  27.  
  28. /**
  29. * Store a newly created resource in storage.
  30. *
  31. * @param \Illuminate\Http\Request $request
  32. * @return \Illuminate\Http\Response
  33. */
  34. public function store(Request $request)
  35. {
  36. $this->validate($request,[
  37. 'title' => 'required|string|max:50',
  38. 'post' => 'required|string|max:50',
  39. ]);
  40.  
  41. $comments = Comment::create([
  42. 'title' => $request->title,
  43. 'post' => $request->post,
  44. ]);
  45.  
  46. return response()->json($comments,200);
  47. }
  48.  
  49. /**
  50. * Display the specified resource.
  51. *
  52. * @param int $id
  53. * @return \Illuminate\Http\Response
  54. */
  55. public function show($id)
  56. {
  57. $comments=Comment::find($id);
  58. if (is_null($comments)) {
  59. return response()->json('not found',404);
  60. }
  61. return response()->json($comments,200);
  62. }
  63.  
  64. /**
  65. * Show the form for editing the specified resource.
  66. *
  67. * @param int $id
  68. * @return \Illuminate\Http\Response
  69. */
  70.  
  71. /**
  72. * Update the specified resource in storage.
  73. *
  74. * @param \Illuminate\Http\Request $request
  75. * @param int $id
  76. * @return \Illuminate\Http\Response
  77. */
  78. public function update(Request $request, $id)
  79. {
  80. $comments=Comments::find($id);
  81. if (!is_null(Input::get('title')))
  82. {
  83. $comments->title=Input::get('title');
  84. }
  85. if (!is_null(Input::get('post')))
  86. {
  87. $comments->post=Input::get('post');
  88. }
  89. $success=$comments->save();
  90. if(!$success){
  91. return response()->json('error update',500);
  92. }
  93. return response()->json('success',201);
  94. }
  95.  
  96. /**
  97. * Remove the specified resource from storage.
  98. *
  99. * @param int $id
  100. * @return \Illuminate\Http\Response
  101. */
  102. public function destroy($id)
  103. {
  104. $comments=Comments::find($id);
  105. if(is_null($comments)){
  106. return response()->json('not found',404);
  107. }
  108. $succes=$comments->delete();
  109. if(!$success){
  110. return response()->json('error delete',500);
  111. }
  112. return response()->json('success',200);
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement