質問編集履歴
9
ニュアンスが分かりづらいかもしれないので変更しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -386,7 +386,7 @@
|
|
386
386
|
|
387
387
|
が出てしまいます。
|
388
388
|
|
389
|
-
おそらくcategory_idも紐付けて登録しないといけないのですが、分かりません。
|
389
|
+
おそらくcategory_idも紐付けて登録しないといけないのですが、どういったコードで修正できるか分かりません。
|
390
390
|
|
391
391
|
|
392
392
|
|
8
表現をわかりやすくしました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -346,7 +346,7 @@
|
|
346
346
|
|
347
347
|
|
348
348
|
|
349
|
-
二つのモデルだったときのCRUD処理は、
|
349
|
+
二つのモデルだったときのCRUD処理は、二つのusersテーブルとdrillsテーブルが常に関係しているようにして
|
350
350
|
|
351
351
|
|
352
352
|
|
7
表現をわかりやすく変えました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -342,7 +342,7 @@
|
|
342
342
|
|
343
343
|
|
344
344
|
|
345
|
-
アプリケーションで行うCRUD処理は
|
345
|
+
アプリケーションで行うCRUD処理は4つのテーブルが常に関係しているようにします。(drillsテーブルにはusersテーブル、categoryテーブルとそれぞれ紐づくuser_id,category_idを与え、drillsテーブルと紐づくdrill_idをproblemsテーブルに与えています。)
|
346
346
|
|
347
347
|
|
348
348
|
|
@@ -362,7 +362,7 @@
|
|
362
362
|
|
363
363
|
|
364
364
|
|
365
|
-
で成功しました.
|
365
|
+
で成功しました.
|
366
366
|
|
367
367
|
|
368
368
|
|
6
具体例を分かりやすくしました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -362,9 +362,13 @@
|
|
362
362
|
|
363
363
|
|
364
364
|
|
365
|
+
で成功しました.
|
366
|
+
|
367
|
+
|
368
|
+
|
365
|
-
|
369
|
+
4つのモデルを全て紐付けると、どのようなコードを実行すればいいか分かりません。
|
370
|
+
|
366
|
-
|
371
|
+
例えば、drillのタイトル(title)と、カテゴリー名(category_name)と、問題一覧(problem1,probelm2,...probelm9)と、登録するユーザー名(user_name)を関連づけて登録する場合
|
367
|
-
|
368
372
|
|
369
373
|
上記の4つのモデルで、
|
370
374
|
|
5
分からない点を明確にしました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -40,11 +40,7 @@
|
|
40
40
|
|
41
41
|
```
|
42
42
|
|
43
|
-
|
44
|
-
|
45
|
-
|
43
|
+
2つのモデルの扱い方はなんとなく理解したので、この2つのモデルをもとに4つのモデルでリレーションを作りたいです。
|
46
|
-
|
47
|
-
|
48
44
|
|
49
45
|
User:Drill 1:多
|
50
46
|
|
@@ -52,19 +48,159 @@
|
|
52
48
|
|
53
49
|
Problem:Drill 1:1
|
54
50
|
|
55
|
-
|
51
|
+
|
56
|
-
|
57
|
-
|
58
|
-
|
52
|
+
|
59
|
-
それぞれの
|
53
|
+
それぞれのmysqlテーブルのプロパティは
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
```users
|
58
|
+
|
59
|
+
id
|
60
|
+
|
61
|
+
name
|
62
|
+
|
63
|
+
```
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
```drills
|
68
|
+
|
69
|
+
id
|
70
|
+
|
71
|
+
title
|
72
|
+
|
73
|
+
user_id
|
74
|
+
|
75
|
+
category_id
|
76
|
+
|
77
|
+
```
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
```category
|
82
|
+
|
83
|
+
id
|
84
|
+
|
85
|
+
category_name
|
86
|
+
|
87
|
+
```
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
```problems
|
92
|
+
|
93
|
+
id
|
94
|
+
|
95
|
+
drill_id
|
96
|
+
|
97
|
+
problem0
|
98
|
+
|
99
|
+
problem1
|
100
|
+
|
101
|
+
...
|
102
|
+
|
103
|
+
problem9
|
104
|
+
|
105
|
+
```
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
です。
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
モデルは
|
60
114
|
|
61
115
|
|
62
116
|
|
63
117
|
```User
|
64
118
|
|
119
|
+
<?php
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
namespace App;
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
use Illuminate\Notifications\Notifiable;
|
128
|
+
|
129
|
+
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
130
|
+
|
131
|
+
use Illuminate\Foundation\Auth\User as Authenticatable;
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
class User extends Authenticatable
|
136
|
+
|
137
|
+
{
|
138
|
+
|
139
|
+
use Notifiable;
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
/**
|
144
|
+
|
145
|
+
* The attributes that are mass assignable.
|
146
|
+
|
147
|
+
*
|
148
|
+
|
149
|
+
* @var array
|
150
|
+
|
151
|
+
*/
|
152
|
+
|
153
|
+
protected $fillable = [
|
154
|
+
|
155
|
+
'name', 'email', 'password',
|
156
|
+
|
65
|
-
|
157
|
+
];
|
158
|
+
|
159
|
+
|
160
|
+
|
66
|
-
|
161
|
+
/**
|
162
|
+
|
163
|
+
* The attributes that should be hidden for arrays.
|
164
|
+
|
165
|
+
*
|
166
|
+
|
167
|
+
* @var array
|
168
|
+
|
67
|
-
|
169
|
+
*/
|
170
|
+
|
171
|
+
protected $hidden = [
|
172
|
+
|
173
|
+
'password', 'remember_token',
|
174
|
+
|
175
|
+
];
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
/**
|
180
|
+
|
181
|
+
* The attributes that should be cast to native types.
|
182
|
+
|
183
|
+
*
|
184
|
+
|
185
|
+
* @var array
|
186
|
+
|
187
|
+
*/
|
188
|
+
|
189
|
+
protected $casts = [
|
190
|
+
|
191
|
+
'email_verified_at' => 'datetime',
|
192
|
+
|
193
|
+
];
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
public function drills(){
|
198
|
+
|
199
|
+
return $this->hasMany('App\Drill');
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
}
|
68
204
|
|
69
205
|
```
|
70
206
|
|
@@ -72,13 +208,57 @@
|
|
72
208
|
|
73
209
|
```Drill
|
74
210
|
|
75
|
-
id
|
76
|
-
|
77
|
-
title
|
78
|
-
|
79
|
-
|
211
|
+
<?php
|
80
|
-
|
212
|
+
|
213
|
+
|
214
|
+
|
81
|
-
|
215
|
+
namespace App;
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
use Illuminate\Database\Eloquent\Model;
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
class Drill extends Model
|
224
|
+
|
225
|
+
{
|
226
|
+
|
227
|
+
// protected $fillable = ['title', 'category_name', 'problem0','problem1','problem2','problem3','problem4','problem5','problem6','problem7','problem8','problem9'];
|
228
|
+
|
229
|
+
protected $fillable = ['title'];
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
public function user(){
|
234
|
+
|
235
|
+
return $this->belongsTo('App\User');
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
public function category(){
|
244
|
+
|
245
|
+
return $this->belongsTo('App\Category');
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
public function problem(){
|
254
|
+
|
255
|
+
return $this->hasOne('App\Problems');
|
256
|
+
|
257
|
+
}
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
|
82
262
|
|
83
263
|
```
|
84
264
|
|
@@ -86,9 +266,37 @@
|
|
86
266
|
|
87
267
|
```Category
|
88
268
|
|
89
|
-
|
269
|
+
<?php
|
270
|
+
|
271
|
+
|
272
|
+
|
90
|
-
|
273
|
+
namespace App;
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
use Illuminate\Database\Eloquent\Model;
|
278
|
+
|
279
|
+
|
280
|
+
|
91
|
-
category
|
281
|
+
class Category extends Model
|
282
|
+
|
283
|
+
{
|
284
|
+
|
285
|
+
protected $fillable = ['category_name'];
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
public function drills(){
|
292
|
+
|
293
|
+
return $this->hasMany('App\Drill');
|
294
|
+
|
295
|
+
}
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
}
|
92
300
|
|
93
301
|
```
|
94
302
|
|
@@ -96,40 +304,6 @@
|
|
96
304
|
|
97
305
|
```Problem
|
98
306
|
|
99
|
-
id
|
100
|
-
|
101
|
-
drill_id
|
102
|
-
|
103
|
-
problem0
|
104
|
-
|
105
|
-
problem1
|
106
|
-
|
107
|
-
...
|
108
|
-
|
109
|
-
problem9
|
110
|
-
|
111
|
-
```
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
にして、insertした時や、データをmysqlからselectする時にそれぞれの関係を保ったまま扱いたいです。
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
どういった考え方でリレーションしたら良いか分からないので、その点よろしくお願いします。
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
追記
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
user:Drill 1:多のモデルのままで実装は終わっています。 エラーというコメントは4つのモデルにしようと追加してダメだった部分です。
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
```User
|
132
|
-
|
133
307
|
<?php
|
134
308
|
|
135
309
|
|
@@ -138,216 +312,78 @@
|
|
138
312
|
|
139
313
|
|
140
314
|
|
141
|
-
use Illuminate\
|
315
|
+
use Illuminate\Database\Eloquent\Model;
|
142
|
-
|
143
|
-
|
316
|
+
|
144
|
-
|
145
|
-
|
317
|
+
|
146
|
-
|
147
|
-
|
148
|
-
|
318
|
+
|
149
|
-
class
|
319
|
+
class Problem extends Model
|
150
320
|
|
151
321
|
{
|
152
322
|
|
153
|
-
use Notifiable;
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
/**
|
158
|
-
|
159
|
-
* The attributes that are mass assignable.
|
160
|
-
|
161
|
-
*
|
162
|
-
|
163
|
-
* @var array
|
164
|
-
|
165
|
-
*/
|
166
|
-
|
167
|
-
protected $fillable = [
|
168
|
-
|
169
|
-
|
323
|
+
protected $fillable = ['problem0','problem1','problem2','problem3','problem4','problem5','problem6','problem7','problem8','problem9'];
|
170
|
-
|
171
|
-
|
324
|
+
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
325
|
+
|
176
|
-
|
177
|
-
|
326
|
+
|
178
|
-
|
179
|
-
|
327
|
+
|
180
|
-
|
181
|
-
|
328
|
+
|
182
|
-
|
183
|
-
*/
|
184
|
-
|
185
|
-
protected $hidden = [
|
186
|
-
|
187
|
-
'password', 'remember_token',
|
188
|
-
|
189
|
-
];
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
/**
|
194
|
-
|
195
|
-
* The attributes that should be cast to native types.
|
196
|
-
|
197
|
-
*
|
198
|
-
|
199
|
-
* @var array
|
200
|
-
|
201
|
-
*/
|
202
|
-
|
203
|
-
protected $casts = [
|
204
|
-
|
205
|
-
'email_verified_at' => 'datetime',
|
206
|
-
|
207
|
-
];
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
public function drill
|
329
|
+
public function drill(){
|
212
|
-
|
330
|
+
|
213
|
-
return $this->
|
331
|
+
return $this->belongsTo('App\Drill');
|
214
332
|
|
215
333
|
}
|
216
334
|
|
217
335
|
}
|
218
336
|
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
//
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
}
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
```
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
```Category
|
282
|
-
|
283
|
-
<?php
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
namespace App;
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
use Illuminate\Database\Eloquent\Model;
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
class Category extends Model
|
296
|
-
|
297
|
-
{
|
298
|
-
|
299
|
-
protected $fillable = ['category_name'];
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
// エラー
|
304
|
-
|
305
|
-
public function drills(){
|
306
|
-
|
307
|
-
return $this->hasMany('App\Drill');
|
308
|
-
|
309
|
-
}
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
}
|
314
|
-
|
315
|
-
```
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
```Problem
|
320
|
-
|
321
|
-
<?php
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
namespace App;
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
use Illuminate\Database\Eloquent\Model;
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
class Problem extends Model
|
334
|
-
|
335
|
-
{
|
336
|
-
|
337
|
-
protected $fillable = ['problem0','problem1','problem2','problem3','problem4','problem5','problem6','problem7','problem8','problem9'];
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
// エラー
|
342
|
-
|
343
|
-
public function drill(){
|
344
|
-
|
345
|
-
return $this->belongsTo('App\Drill');
|
346
|
-
|
347
|
-
}
|
348
|
-
|
349
|
-
}
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
```
|
337
|
+
|
338
|
+
|
339
|
+
```
|
340
|
+
|
341
|
+
このような感じです。
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
アプリケーションで行うCRUD処理は全てDrillモデルを起点にして行います。(drillsテーブルにはusersテーブル、categoryテーブルとそれぞれ紐づくuser_id,category_idを与え、drillsテーブルと紐づくdrill_idをproblemsテーブルに与えています。)
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
二つのモデルだったときのCRUD処理は、
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
```ここに言語を入力
|
354
|
+
|
355
|
+
Auth::user()->drills()->save($drill->fill($request->all())); //登録
|
356
|
+
|
357
|
+
Auth::user()->drills()->find($id)->delete(); //消去
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
```
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
で成功しましたが、4つのモデルを全て紐付けると、どのようなコードを実行すればいいか分かりません。
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
上記の4つのモデルで、
|
370
|
+
|
371
|
+
```ここに言語を入力
|
372
|
+
|
373
|
+
Auth::user()->drills()->save($drill->fill($request->all())); //登録
|
374
|
+
|
375
|
+
|
376
|
+
|
377
|
+
```
|
378
|
+
|
379
|
+
を行うと、General error: 1364 Field 'category_id' doesn't have a default value (SQL: insert into `drills` (`title`, `user_id`, `updated_at`, `created_at`) values (aaa, 1, 2020-03-19 03:01:58, 2020-03-19 03:01:58))
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
が出てしまいます。
|
384
|
+
|
385
|
+
おそらくcategory_idも紐付けて登録しないといけないのですが、分かりません。
|
386
|
+
|
387
|
+
|
388
|
+
|
389
|
+
よろしくお願いします。
|
4
drillの一部を複数形にしました
test
CHANGED
File without changes
|
test
CHANGED
@@ -208,146 +208,146 @@
|
|
208
208
|
|
209
209
|
|
210
210
|
|
211
|
+
public function drills(){
|
212
|
+
|
213
|
+
return $this->hasMany('App\Drill');
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
```
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
```Drill
|
224
|
+
|
225
|
+
<?php
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
namespace App;
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
use Illuminate\Database\Eloquent\Model;
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
class Drill extends Model
|
238
|
+
|
239
|
+
{
|
240
|
+
|
241
|
+
// protected $fillable = ['title', 'category_name', 'problem0','problem1','problem2','problem3','problem4','problem5','problem6','problem7','problem8','problem9'];
|
242
|
+
|
243
|
+
protected $fillable = ['title'];
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
public function user(){
|
248
|
+
|
249
|
+
return $this->belongsTo('App\User');
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
// エラー
|
256
|
+
|
257
|
+
public function category(){
|
258
|
+
|
259
|
+
return $this->belongsTo('App\Category');
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
// エラー
|
266
|
+
|
267
|
+
public function problem(){
|
268
|
+
|
269
|
+
return $this->hasOne('App\Problems');
|
270
|
+
|
271
|
+
}
|
272
|
+
|
273
|
+
}
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
```
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
```Category
|
282
|
+
|
283
|
+
<?php
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
namespace App;
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
use Illuminate\Database\Eloquent\Model;
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
class Category extends Model
|
296
|
+
|
297
|
+
{
|
298
|
+
|
299
|
+
protected $fillable = ['category_name'];
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
// エラー
|
304
|
+
|
305
|
+
public function drills(){
|
306
|
+
|
307
|
+
return $this->hasMany('App\Drill');
|
308
|
+
|
309
|
+
}
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
}
|
314
|
+
|
315
|
+
```
|
316
|
+
|
317
|
+
|
318
|
+
|
319
|
+
```Problem
|
320
|
+
|
321
|
+
<?php
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
namespace App;
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
use Illuminate\Database\Eloquent\Model;
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
class Problem extends Model
|
334
|
+
|
335
|
+
{
|
336
|
+
|
337
|
+
protected $fillable = ['problem0','problem1','problem2','problem3','problem4','problem5','problem6','problem7','problem8','problem9'];
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
// エラー
|
342
|
+
|
211
343
|
public function drill(){
|
212
344
|
|
213
|
-
return $this->
|
345
|
+
return $this->belongsTo('App\Drill');
|
214
346
|
|
215
347
|
}
|
216
348
|
|
217
349
|
}
|
218
350
|
|
351
|
+
|
352
|
+
|
219
|
-
```
|
353
|
+
```
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
```Drill
|
224
|
-
|
225
|
-
<?php
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
namespace App;
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
use Illuminate\Database\Eloquent\Model;
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
class Drill extends Model
|
238
|
-
|
239
|
-
{
|
240
|
-
|
241
|
-
// protected $fillable = ['title', 'category_name', 'problem0','problem1','problem2','problem3','problem4','problem5','problem6','problem7','problem8','problem9'];
|
242
|
-
|
243
|
-
protected $fillable = ['title'];
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
public function user(){
|
248
|
-
|
249
|
-
return $this->belongsTo('App\User');
|
250
|
-
|
251
|
-
}
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
// エラー
|
256
|
-
|
257
|
-
public function category(){
|
258
|
-
|
259
|
-
return $this->belongsTo('App\Category');
|
260
|
-
|
261
|
-
}
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
// エラー
|
266
|
-
|
267
|
-
public function drill(){
|
268
|
-
|
269
|
-
return $this->hasOne('App\Problems');
|
270
|
-
|
271
|
-
}
|
272
|
-
|
273
|
-
}
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
```
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
```Category
|
282
|
-
|
283
|
-
<?php
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
namespace App;
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
use Illuminate\Database\Eloquent\Model;
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
class Category extends Model
|
296
|
-
|
297
|
-
{
|
298
|
-
|
299
|
-
protected $fillable = ['category_name'];
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
// エラー
|
304
|
-
|
305
|
-
public function drill(){
|
306
|
-
|
307
|
-
return $this->hasMany('App\Drill');
|
308
|
-
|
309
|
-
}
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
}
|
314
|
-
|
315
|
-
```
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
```Problem
|
320
|
-
|
321
|
-
<?php
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
namespace App;
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
use Illuminate\Database\Eloquent\Model;
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
class Problem extends Model
|
334
|
-
|
335
|
-
{
|
336
|
-
|
337
|
-
protected $fillable = ['problem0','problem1','problem2','problem3','problem4','problem5','problem6','problem7','problem8','problem9'];
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
// エラー
|
342
|
-
|
343
|
-
public function drill(){
|
344
|
-
|
345
|
-
return $this->belongsTo('App\Drill');
|
346
|
-
|
347
|
-
}
|
348
|
-
|
349
|
-
}
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
```
|
3
追加でDrillを修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -208,7 +208,7 @@
|
|
208
208
|
|
209
209
|
|
210
210
|
|
211
|
-
public function drill
|
211
|
+
public function drill(){
|
212
212
|
|
213
213
|
return $this->hasMany('App\Drill');
|
214
214
|
|
@@ -264,7 +264,7 @@
|
|
264
264
|
|
265
265
|
// エラー
|
266
266
|
|
267
|
-
public function drill
|
267
|
+
public function drill(){
|
268
268
|
|
269
269
|
return $this->hasOne('App\Problems');
|
270
270
|
|
@@ -302,7 +302,7 @@
|
|
302
302
|
|
303
303
|
// エラー
|
304
304
|
|
305
|
-
public function drill
|
305
|
+
public function drill(){
|
306
306
|
|
307
307
|
return $this->hasMany('App\Drill');
|
308
308
|
|
@@ -340,7 +340,7 @@
|
|
340
340
|
|
341
341
|
// エラー
|
342
342
|
|
343
|
-
public function drill
|
343
|
+
public function drill(){
|
344
344
|
|
345
345
|
return $this->belongsTo('App\Drill');
|
346
346
|
|
2
メソッド、クラスをDrillに変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -20,7 +20,7 @@
|
|
20
20
|
|
21
21
|
|
22
22
|
|
23
|
-
```Drill
|
23
|
+
```Drill
|
24
24
|
|
25
25
|
コード
|
26
26
|
|
@@ -42,15 +42,15 @@
|
|
42
42
|
|
43
43
|
|
44
44
|
|
45
|
-
そこでDrill
|
45
|
+
そこでDrillをDrill,Category, Problemという3つのモデルに分割して、全部で4つのモデルをリレーションできるかがよく理解できません。
|
46
|
-
|
47
|
-
|
48
|
-
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
-
User:Drill
|
49
|
+
User:Drill 1:多
|
50
|
-
|
50
|
+
|
51
|
-
Category:Drill
|
51
|
+
Category:Drill: 1:多
|
52
|
-
|
52
|
+
|
53
|
-
Problem:Drill
|
53
|
+
Problem:Drill 1:1
|
54
54
|
|
55
55
|
なのですが、ポリモーフィックを使ったらいいのかドキュメントを読みましたが、今一つ理解できません。
|
56
56
|
|
@@ -70,7 +70,7 @@
|
|
70
70
|
|
71
71
|
|
72
72
|
|
73
|
-
```Drill
|
73
|
+
```Drill
|
74
74
|
|
75
75
|
id
|
76
76
|
|
1
モデルを追加しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -117,3 +117,237 @@
|
|
117
117
|
|
118
118
|
|
119
119
|
どういった考え方でリレーションしたら良いか分からないので、その点よろしくお願いします。
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
追記
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
user:Drill 1:多のモデルのままで実装は終わっています。 エラーというコメントは4つのモデルにしようと追加してダメだった部分です。
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
```User
|
132
|
+
|
133
|
+
<?php
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
namespace App;
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
use Illuminate\Notifications\Notifiable;
|
142
|
+
|
143
|
+
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
144
|
+
|
145
|
+
use Illuminate\Foundation\Auth\User as Authenticatable;
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
class User extends Authenticatable
|
150
|
+
|
151
|
+
{
|
152
|
+
|
153
|
+
use Notifiable;
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
/**
|
158
|
+
|
159
|
+
* The attributes that are mass assignable.
|
160
|
+
|
161
|
+
*
|
162
|
+
|
163
|
+
* @var array
|
164
|
+
|
165
|
+
*/
|
166
|
+
|
167
|
+
protected $fillable = [
|
168
|
+
|
169
|
+
'name', 'email', 'password',
|
170
|
+
|
171
|
+
];
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
/**
|
176
|
+
|
177
|
+
* The attributes that should be hidden for arrays.
|
178
|
+
|
179
|
+
*
|
180
|
+
|
181
|
+
* @var array
|
182
|
+
|
183
|
+
*/
|
184
|
+
|
185
|
+
protected $hidden = [
|
186
|
+
|
187
|
+
'password', 'remember_token',
|
188
|
+
|
189
|
+
];
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
/**
|
194
|
+
|
195
|
+
* The attributes that should be cast to native types.
|
196
|
+
|
197
|
+
*
|
198
|
+
|
199
|
+
* @var array
|
200
|
+
|
201
|
+
*/
|
202
|
+
|
203
|
+
protected $casts = [
|
204
|
+
|
205
|
+
'email_verified_at' => 'datetime',
|
206
|
+
|
207
|
+
];
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
public function drills(){
|
212
|
+
|
213
|
+
return $this->hasMany('App\Drill');
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
```
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
```Drill
|
224
|
+
|
225
|
+
<?php
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
namespace App;
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
use Illuminate\Database\Eloquent\Model;
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
class Drill extends Model
|
238
|
+
|
239
|
+
{
|
240
|
+
|
241
|
+
// protected $fillable = ['title', 'category_name', 'problem0','problem1','problem2','problem3','problem4','problem5','problem6','problem7','problem8','problem9'];
|
242
|
+
|
243
|
+
protected $fillable = ['title'];
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
public function user(){
|
248
|
+
|
249
|
+
return $this->belongsTo('App\User');
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
// エラー
|
256
|
+
|
257
|
+
public function category(){
|
258
|
+
|
259
|
+
return $this->belongsTo('App\Category');
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
// エラー
|
266
|
+
|
267
|
+
public function drills(){
|
268
|
+
|
269
|
+
return $this->hasOne('App\Problems');
|
270
|
+
|
271
|
+
}
|
272
|
+
|
273
|
+
}
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
```
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
```Category
|
282
|
+
|
283
|
+
<?php
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
namespace App;
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
use Illuminate\Database\Eloquent\Model;
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
class Category extends Model
|
296
|
+
|
297
|
+
{
|
298
|
+
|
299
|
+
protected $fillable = ['category_name'];
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
// エラー
|
304
|
+
|
305
|
+
public function drills(){
|
306
|
+
|
307
|
+
return $this->hasMany('App\Drill');
|
308
|
+
|
309
|
+
}
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
}
|
314
|
+
|
315
|
+
```
|
316
|
+
|
317
|
+
|
318
|
+
|
319
|
+
```Problem
|
320
|
+
|
321
|
+
<?php
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
namespace App;
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
use Illuminate\Database\Eloquent\Model;
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
class Problem extends Model
|
334
|
+
|
335
|
+
{
|
336
|
+
|
337
|
+
protected $fillable = ['problem0','problem1','problem2','problem3','problem4','problem5','problem6','problem7','problem8','problem9'];
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
// エラー
|
342
|
+
|
343
|
+
public function drills(){
|
344
|
+
|
345
|
+
return $this->belongsTo('App\Drill');
|
346
|
+
|
347
|
+
}
|
348
|
+
|
349
|
+
}
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
```
|