質問編集履歴

1

具体的なコードとpostmanのスクショを追加

2017/10/01 03:03

投稿

tm_rhcp
tm_rhcp

スコア17

test CHANGED
File without changes
test CHANGED
@@ -53,3 +53,321 @@
53
53
 
54
54
 
55
55
  なにかご存知でしたらよろしくお願い致します。
56
+
57
+
58
+
59
+
60
+
61
+ ▼▼▼▼▼ 以下、追記(2017.10月1日) ▼▼▼▼
62
+
63
+ 具体的なコードをお知らせします。
64
+
65
+
66
+
67
+ ▼app/Http/Controllers/Book/BookController.php
68
+
69
+ ```
70
+
71
+ <?php
72
+
73
+
74
+
75
+ namespace App\Http\Controllers\Book;
76
+
77
+
78
+
79
+ use App\Book;
80
+
81
+ use Illuminate\Http\Request;
82
+
83
+ use App\Http\Controllers\ApiController;
84
+
85
+
86
+
87
+ class BookController extends ApiController
88
+
89
+ {
90
+
91
+ public function index()
92
+
93
+ {
94
+
95
+ $books = Book::all();
96
+
97
+ return $this->showAll($books);
98
+
99
+ }
100
+
101
+
102
+
103
+ public function create()
104
+
105
+ {
106
+
107
+ //
108
+
109
+ }
110
+
111
+
112
+
113
+ public function store(Request $request)
114
+
115
+ {
116
+
117
+ $rules = [
118
+
119
+ 'title' => 'required',
120
+
121
+ 'author' => 'required',
122
+
123
+ 'isbn' => 'required'
124
+
125
+ ];
126
+
127
+ $this->validate($request, $rules);
128
+
129
+ $data = $request->all();
130
+
131
+ $book = Book::create($data);
132
+
133
+ return $this->showOne($book, 201);
134
+
135
+ }
136
+
137
+
138
+
139
+ public function show(Book $book)
140
+
141
+ {
142
+
143
+ return $this->showOne($book);
144
+
145
+ }
146
+
147
+ ```
148
+
149
+
150
+
151
+ ▼app/Traits/ApiResponser.php
152
+
153
+ ```
154
+
155
+ <?php
156
+
157
+
158
+
159
+ namespace App\Traits;
160
+
161
+
162
+
163
+ use Illuminate\Support\Collection;
164
+
165
+ use Illuminate\Database\Eloquent\Model;
166
+
167
+
168
+
169
+ trait ApiResponser
170
+
171
+ {
172
+
173
+ private function successResponse($data, $code)
174
+
175
+ {
176
+
177
+ return response()->json($data, $code);
178
+
179
+ }
180
+
181
+
182
+
183
+ protected function errorResponse($message, $code)
184
+
185
+ {
186
+
187
+ return response()->json(['error'=>$message, 'code'=>$code], $code);
188
+
189
+ }
190
+
191
+
192
+
193
+ protected function showAll(Collection $collection, $code = 200)
194
+
195
+ {
196
+
197
+ return $this->successResponse(['data'=>$collection], $code);
198
+
199
+ }
200
+
201
+
202
+
203
+ protected function showOne(Model $model, $code = 200)
204
+
205
+ {
206
+
207
+ return $this->successResponse(['data'=>$model], $code);
208
+
209
+ }
210
+
211
+ }
212
+
213
+ ```
214
+
215
+
216
+
217
+ ▼app\Http\Controllers\ApiController
218
+
219
+ ```
220
+
221
+ <?php
222
+
223
+
224
+
225
+ namespace App\Http\Controllers;
226
+
227
+
228
+
229
+ use App\Traits\ApiResponser;
230
+
231
+ use Illuminate\Http\Request;
232
+
233
+
234
+
235
+ class ApiController extends Controller
236
+
237
+ {
238
+
239
+ use ApiResponser;
240
+
241
+ }
242
+
243
+ ```
244
+
245
+
246
+
247
+ ▼routes/api.php
248
+
249
+ ```
250
+
251
+ <?php
252
+
253
+
254
+
255
+ use Illuminate\Http\Request;
256
+
257
+
258
+
259
+ /*
260
+
261
+ * User
262
+
263
+ */
264
+
265
+ Route::resource('users', 'User\UserController', ['except' => ['create', 'edit']]);
266
+
267
+
268
+
269
+ /*
270
+
271
+ * Book
272
+
273
+ */
274
+
275
+ Route::resource('books', 'Book\BookController');
276
+
277
+ ```
278
+
279
+
280
+
281
+ ▼マイグレーションファイル
282
+
283
+ ```
284
+
285
+ <?php
286
+
287
+
288
+
289
+ use Illuminate\Support\Facades\Schema;
290
+
291
+ use Illuminate\Database\Schema\Blueprint;
292
+
293
+ use Illuminate\Database\Migrations\Migration;
294
+
295
+
296
+
297
+ class CreateBooksTable extends Migration
298
+
299
+ {
300
+
301
+ /**
302
+
303
+ * Run the migrations.
304
+
305
+ *
306
+
307
+ * @return void
308
+
309
+ */
310
+
311
+ public function up()
312
+
313
+ {
314
+
315
+ Schema::create('books', function (Blueprint $table) {
316
+
317
+ $table->increments('id');
318
+
319
+ $table->string('title');
320
+
321
+ $table->string('author');
322
+
323
+ $table->string('isbn');
324
+
325
+ $table->timestamps();
326
+
327
+ $table->softDeletes();
328
+
329
+ });
330
+
331
+ }
332
+
333
+
334
+
335
+ /**
336
+
337
+ * Reverse the migrations.
338
+
339
+ *
340
+
341
+ * @return void
342
+
343
+ */
344
+
345
+ public function down()
346
+
347
+ {
348
+
349
+ Schema::dropIfExists('books');
350
+
351
+ }
352
+
353
+ }
354
+
355
+ ```
356
+
357
+
358
+
359
+ postmanで送っているものは以下の通りです(bodyに返ってきているものはtinkerで直接保存できたものです)
360
+
361
+ ![イメージ説明](a6450c487364c3e26755cbe351adcbf2.jpeg)
362
+
363
+
364
+
365
+ ちなみに今回はudemyの下記の口座を見ながら作っていまして、現在はセクション27付近まで見つつ実装しています。
366
+
367
+ ▼参考にしている講座
368
+
369
+ https://www.udemy.com/restful-api-with-laravel-php-homestead-passport-hateoas/learn/v4/overview
370
+
371
+ ▼上記コースの完成版のコード
372
+
373
+ https://github.com/JuanDMeGon/RESTful-API-with-Laravel-Definitive-Guide