質問編集履歴

1

modelのソースを追加

2019/09/26 22:59

投稿

nikoniko
nikoniko

スコア11

test CHANGED
File without changes
test CHANGED
@@ -214,6 +214,100 @@
214
214
 
215
215
 
216
216
 
217
+ ```Model
218
+
219
+ <?php
220
+
221
+
222
+
223
+ namespace App;
224
+
225
+
226
+
227
+ use Illuminate\Database\Eloquent\Model;
228
+
229
+
230
+
231
+ class Category extends Model
232
+
233
+ {
234
+
235
+ protected $fillable = ['id', 'name'];
236
+
237
+
238
+
239
+ //学習記録を取得
240
+
241
+ public function records(){
242
+
243
+ return $this->hasMany('App\Record');
244
+
245
+ }
246
+
247
+ }
248
+
249
+
250
+
251
+
252
+
253
+ ```
254
+
255
+
256
+
257
+
258
+
259
+ ```Model
260
+
261
+ <?php
262
+
263
+
264
+
265
+ namespace App;
266
+
267
+
268
+
269
+ use Illuminate\Database\Eloquent\Model;
270
+
271
+
272
+
273
+ class Record extends Model
274
+
275
+ {
276
+
277
+
278
+
279
+ protected $fillable = [
280
+
281
+ 'id',
282
+
283
+ 'category_id',
284
+
285
+ 'time'
286
+
287
+ ];
288
+
289
+
290
+
291
+ //このカテゴリを所有する学習記録を取得
292
+
293
+ public function category(){
294
+
295
+ return $this->belongsTo(Category::class, 'category_id');
296
+
297
+ }
298
+
299
+ }
300
+
301
+
302
+
303
+ }
304
+
305
+
306
+
307
+ ```
308
+
309
+
310
+
217
311
  ```Route
218
312
 
219
313
  Route::match(['get', 'post'], '/', 'RecordsController@index');