Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use App\Comment;
- use Illuminate\Http\Request;
- class CommentController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- $comments=Comment::all();
- return response()->json($comments,200);
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return \Illuminate\Http\Response
- */
- /**
- * Store a newly created resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function store(Request $request)
- {
- $this->validate($request,[
- 'title' => 'required|string|max:50',
- 'post' => 'required|string|max:50',
- ]);
- $comments = Comment::create([
- 'title' => $request->title,
- 'post' => $request->post,
- ]);
- return response()->json($comments,200);
- }
- /**
- * Display the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function show($id)
- {
- $comments=Comment::find($id);
- if (is_null($comments)) {
- return response()->json('not found',404);
- }
- return response()->json($comments,200);
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function update(Request $request, $id)
- {
- $comments=Comments::find($id);
- if (!is_null(Input::get('title')))
- {
- $comments->title=Input::get('title');
- }
- if (!is_null(Input::get('post')))
- {
- $comments->post=Input::get('post');
- }
- $success=$comments->save();
- if(!$success){
- return response()->json('error update',500);
- }
- return response()->json('success',201);
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function destroy($id)
- {
- $comments=Comments::find($id);
- if(is_null($comments)){
- return response()->json('not found',404);
- }
- $succes=$comments->delete();
- if(!$success){
- return response()->json('error delete',500);
- }
- return response()->json('success',200);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement