@@ -14,9 +14,9 @@ class BookController extends Controller
1414 */
1515 public function index ()
1616 {
17- $ book = Book::all ();
17+ $ books = Book::all ();
1818
19- return response ()->json ($ book );
19+ return response ()->json ($ books );
2020 }
2121
2222 /**
@@ -32,38 +32,38 @@ public function create()
3232 /**
3333 * Store a newly created resource in storage.
3434 *
35- * @param \Illuminate\Http\Request $request
35+ * @param \Illuminate\Http\Request $request
3636 * @return \Illuminate\Http\Response
3737 */
3838 public function store (Request $ request )
3939 {
40- $ this ->validate ($ request , [
41- 'author ' => 'required ' ,
42- 'description ' => 'required '
40+ $ book = $ request ->validate ([
41+ 'author ' => 'required ' ,
42+ 'description ' => 'required '
4343 ]);
4444
45- $ book = Book::create ($ request -> all () );
45+ $ books = Book::create ($ book );
4646
47- return response ()->json ($ book );
47+ return response ()->json ($ books );
4848 }
4949
5050 /**
5151 * Display the specified resource.
5252 *
53- * @param int $id
53+ * @param int $id
5454 * @return \Illuminate\Http\Response
5555 */
5656 public function show ($ id )
5757 {
58- $ book = Book::find ($ id );
58+ $ book = Book::findOrFail ($ id );
5959
6060 return response ()->json ($ book );
6161 }
6262
6363 /**
6464 * Show the form for editing the specified resource.
6565 *
66- * @param int $id
66+ * @param int $id
6767 * @return \Illuminate\Http\Response
6868 */
6969 public function edit ($ id )
@@ -74,28 +74,32 @@ public function edit($id)
7474 /**
7575 * Update the specified resource in storage.
7676 *
77- * @param \Illuminate\Http\Request $request
78- * @param int $id
77+ * @param \Illuminate\Http\Request $request
78+ * @param int $id
7979 * @return \Illuminate\Http\Response
8080 */
8181 public function update (Request $ request , $ id )
8282 {
83- $ book = new Book ;
84- $ book ->author = $ request ->input ('author ' );
85- $ book ->description = $ request ->input ('description ' );
86- $ book ->save ();
83+ $ data = $ request ->validate ([
84+ 'author ' => 'required ' ,
85+ 'description ' => 'required '
86+ ]);
87+
88+ $ book = Book::findOrFail ($ id );
89+ $ book ->update ($ data );
90+
8791 return response ()->json ($ book );
8892 }
8993
9094 /**
9195 * Remove the specified resource from storage.
9296 *
93- * @param int $id
97+ * @param int $id
9498 * @return \Illuminate\Http\Response
9599 */
96100 public function destroy ($ id )
97101 {
98- $ book = Book::find ($ id );
102+ $ book = Book::findOrFail ($ id );
99103 $ book ->delete ();
100104
101105 return response ()->json ($ book );
0 commit comments