質問編集履歴
6
誤字
test
CHANGED
File without changes
|
test
CHANGED
@@ -172,7 +172,7 @@
|
|
172
172
|
|
173
173
|
{
|
174
174
|
|
175
|
-
return $this->hasOne('App\Models\Se
|
175
|
+
return $this->hasOne('App\Models\SampleLink');
|
176
176
|
|
177
177
|
}
|
178
178
|
|
5
質問の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -48,7 +48,7 @@
|
|
48
48
|
|
49
49
|
$article = factory(Article::class)->create()->each(function ($article) {
|
50
50
|
|
51
|
-
$article->
|
51
|
+
$article->sampleLink()->save(factory(SampleLink::class)->make());
|
52
52
|
|
53
53
|
});
|
54
54
|
|
@@ -168,11 +168,11 @@
|
|
168
168
|
|
169
169
|
|
170
170
|
|
171
|
-
public function
|
171
|
+
public function sampleLink(): HasOne
|
172
|
-
|
172
|
+
|
173
|
-
{
|
173
|
+
{
|
174
|
-
|
174
|
+
|
175
|
-
return $this->hasOne('App\Models\
|
175
|
+
return $this->hasOne('App\Models\SewsLink');
|
176
176
|
|
177
177
|
}
|
178
178
|
|
@@ -216,9 +216,7 @@
|
|
216
216
|
|
217
217
|
|
218
218
|
|
219
|
-
###Models/
|
219
|
+
###Models/User.php
|
220
|
-
|
221
|
-
|
222
220
|
|
223
221
|
```
|
224
222
|
|
@@ -230,33 +228,59 @@
|
|
230
228
|
|
231
229
|
|
232
230
|
|
233
|
-
use
|
231
|
+
use App\Mail\BareMail;
|
234
|
-
|
232
|
+
|
235
|
-
use Illuminate\Database\Eloquent\Relations\
|
233
|
+
use Illuminate\Database\Eloquent\Relations\HasMany;
|
234
|
+
|
236
|
-
|
235
|
+
use Illuminate\Foundation\Auth\User as Authenticatable;
|
236
|
+
|
237
|
-
use Illuminate\
|
237
|
+
use Illuminate\Notifications\Notifiable;
|
238
|
-
|
239
|
-
|
240
|
-
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
-
class
|
241
|
+
class User extends Authenticatable
|
242
242
|
|
243
243
|
{
|
244
244
|
|
245
|
+
use Notifiable;
|
246
|
+
|
247
|
+
|
248
|
+
|
245
249
|
protected $fillable = [
|
246
250
|
|
247
|
-
'ne
|
251
|
+
'name', 'email', 'introduction', 'password',
|
248
|
-
|
252
|
+
|
249
|
-
];
|
253
|
+
];
|
254
|
+
|
255
|
+
|
256
|
+
|
250
|
-
|
257
|
+
protected $hidden = [
|
258
|
+
|
251
|
-
|
259
|
+
'password', 'remember_token',
|
260
|
+
|
252
|
-
|
261
|
+
];
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
protected $casts = [
|
266
|
+
|
267
|
+
'email_verified_at' => 'datetime',
|
268
|
+
|
269
|
+
];
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
//articleとのリレーション
|
274
|
+
|
253
|
-
public function article():
|
275
|
+
public function articles(): HasMany
|
254
|
-
|
276
|
+
|
255
|
-
{
|
277
|
+
{
|
256
|
-
|
278
|
+
|
257
|
-
return $this->
|
279
|
+
return $this->hasMany('App\Models\Article');
|
258
|
-
|
280
|
+
|
259
|
-
}
|
281
|
+
}
|
282
|
+
|
283
|
+
//以下略
|
260
284
|
|
261
285
|
}
|
262
286
|
|
@@ -264,154 +288,44 @@
|
|
264
288
|
|
265
289
|
|
266
290
|
|
267
|
-
###
|
291
|
+
###UserFactory.php
|
268
|
-
|
269
|
-
実際には``news``と``url``のデータは外部APIから取得して保存しています。
|
270
292
|
|
271
293
|
```
|
272
294
|
|
273
295
|
<?php
|
274
296
|
|
275
|
-
|
297
|
+
|
276
|
-
|
298
|
+
|
277
|
-
use App\Models\
|
299
|
+
use App\Models\User;
|
278
300
|
|
279
301
|
use Faker\Generator as Faker;
|
280
302
|
|
281
|
-
|
303
|
+
use Illuminate\Support\Str;
|
282
|
-
|
304
|
+
|
305
|
+
|
306
|
+
|
283
|
-
$factory->define(
|
307
|
+
$factory->define(User::class, function (Faker $faker) {
|
284
308
|
|
285
309
|
return [
|
286
310
|
|
287
|
-
'a
|
311
|
+
'name' => $faker->name,
|
288
|
-
|
312
|
+
|
289
|
-
|
313
|
+
'email' => $faker->unique()->safeEmail,
|
290
|
-
|
291
|
-
|
314
|
+
|
292
|
-
|
293
|
-
'n
|
315
|
+
'introduction' => $faker->text(200),
|
316
|
+
|
294
|
-
|
317
|
+
'image' => $faker->imageUrl($randomize = true, $word = null),
|
318
|
+
|
319
|
+
'email_verified_at' => now(),
|
320
|
+
|
321
|
+
'password' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', // password
|
322
|
+
|
295
|
-
'
|
323
|
+
'remember_token' => Str::random(10),
|
296
324
|
|
297
325
|
];
|
298
326
|
|
299
327
|
});
|
300
328
|
|
329
|
+
|
330
|
+
|
301
|
-
```
|
331
|
+
```
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
###Models/User.php
|
306
|
-
|
307
|
-
```
|
308
|
-
|
309
|
-
<?php
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
namespace App\Models;
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
use App\Mail\BareMail;
|
318
|
-
|
319
|
-
use Illuminate\Database\Eloquent\Relations\HasMany;
|
320
|
-
|
321
|
-
use Illuminate\Foundation\Auth\User as Authenticatable;
|
322
|
-
|
323
|
-
use Illuminate\Notifications\Notifiable;
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
class User extends Authenticatable
|
328
|
-
|
329
|
-
{
|
330
|
-
|
331
|
-
use Notifiable;
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
protected $fillable = [
|
336
|
-
|
337
|
-
'name', 'email', 'introduction', 'password',
|
338
|
-
|
339
|
-
];
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
protected $hidden = [
|
344
|
-
|
345
|
-
'password', 'remember_token',
|
346
|
-
|
347
|
-
];
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
protected $casts = [
|
352
|
-
|
353
|
-
'email_verified_at' => 'datetime',
|
354
|
-
|
355
|
-
];
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
//articleとのリレーション
|
360
|
-
|
361
|
-
public function articles(): HasMany
|
362
|
-
|
363
|
-
{
|
364
|
-
|
365
|
-
return $this->hasMany('App\Models\Article');
|
366
|
-
|
367
|
-
}
|
368
|
-
|
369
|
-
//以下略
|
370
|
-
|
371
|
-
}
|
372
|
-
|
373
|
-
```
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
###UserFactory.php
|
378
|
-
|
379
|
-
```
|
380
|
-
|
381
|
-
<?php
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
use App\Models\User;
|
386
|
-
|
387
|
-
use Faker\Generator as Faker;
|
388
|
-
|
389
|
-
use Illuminate\Support\Str;
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
$factory->define(User::class, function (Faker $faker) {
|
394
|
-
|
395
|
-
return [
|
396
|
-
|
397
|
-
'name' => $faker->name,
|
398
|
-
|
399
|
-
'email' => $faker->unique()->safeEmail,
|
400
|
-
|
401
|
-
'introduction' => $faker->text(200),
|
402
|
-
|
403
|
-
'image' => $faker->imageUrl($randomize = true, $word = null),
|
404
|
-
|
405
|
-
'email_verified_at' => now(),
|
406
|
-
|
407
|
-
'password' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', // password
|
408
|
-
|
409
|
-
'remember_token' => Str::random(10),
|
410
|
-
|
411
|
-
];
|
412
|
-
|
413
|
-
});
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
```
|
4
質問の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -415,105 +415,3 @@
|
|
415
415
|
|
416
416
|
|
417
417
|
```
|
418
|
-
|
419
|
-
##各テーブル構造
|
420
|
-
|
421
|
-
各テーブルのcreated_atとupdated_atは省略しています。
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
###articles table
|
426
|
-
|
427
|
-
|id|user_id|body|
|
428
|
-
|
429
|
-
|:--|:--:|--:|
|
430
|
-
|
431
|
-
|1|1|サンプル|
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
```
|
436
|
-
|
437
|
-
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
438
|
-
|
439
|
-
`user_id` bigint(20) unsigned NOT NULL,
|
440
|
-
|
441
|
-
`body` text NOT NULL,
|
442
|
-
|
443
|
-
PRIMARY KEY (`id`),
|
444
|
-
|
445
|
-
KEY `articles_user_id_foreign` (`user_id`),
|
446
|
-
|
447
|
-
CONSTRAINT `articles_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
|
448
|
-
|
449
|
-
```
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
###users table
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|id|name|email|introduction|image|email_verified_at|password|remember_token|
|
458
|
-
|
459
|
-
|:--|:--:|--:|--:|--:|--:|--:|
|
460
|
-
|
461
|
-
|1|ゲストユーザー|sample@sample.com|NULL|NULL|NULL|xxxx|xxxx|
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
```
|
466
|
-
|
467
|
-
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
468
|
-
|
469
|
-
`name` varchar(255) NOT NULL,
|
470
|
-
|
471
|
-
`email` varchar(255) NOT NULL,
|
472
|
-
|
473
|
-
`introduction` varchar(255) DEFAULT NULL,
|
474
|
-
|
475
|
-
`image` varchar(255) DEFAULT NULL,
|
476
|
-
|
477
|
-
`email_verified_at` timestamp NULL DEFAULT NULL,
|
478
|
-
|
479
|
-
`password` varchar(255) DEFAULT NULL,
|
480
|
-
|
481
|
-
`remember_token` varchar(100) DEFAULT NULL,
|
482
|
-
|
483
|
-
PRIMARY KEY (`id`),
|
484
|
-
|
485
|
-
UNIQUE KEY `users_name_unique` (`name`),
|
486
|
-
|
487
|
-
UNIQUE KEY `users_email_unique` (`email`)
|
488
|
-
|
489
|
-
```
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
###news_link table
|
494
|
-
|
495
|
-
|id|article_id|news|url|
|
496
|
-
|
497
|
-
|:--|:--:|--:|--:|
|
498
|
-
|
499
|
-
|1|1|サンプル|https://www.サンプル.co.jp|
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
```
|
504
|
-
|
505
|
-
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
506
|
-
|
507
|
-
`article_id` bigint(20) unsigned NOT NULL,
|
508
|
-
|
509
|
-
`news` varchar(255) NOT NULL,
|
510
|
-
|
511
|
-
`url` varchar(255) NOT NULL,
|
512
|
-
|
513
|
-
PRIMARY KEY (`id`),
|
514
|
-
|
515
|
-
KEY `news_links_article_id_foreign` (`article_id`),
|
516
|
-
|
517
|
-
CONSTRAINT `news_links_article_id_foreign` FOREIGN KEY (`article_id`) REFERENCES `articles` (`id`) ON DELETE CASCADE
|
518
|
-
|
519
|
-
```
|
3
質問の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,9 +8,9 @@
|
|
8
8
|
|
9
9
|
|
10
10
|
|
11
|
-
エラー文を読むと、userのデータがないと言われているような気がしますが、私としては
|
11
|
+
エラー文を読むと、userのデータがないと言われているような気がしますが、私としては
|
12
|
-
|
12
|
+
|
13
|
-
ArticleFacotoryにて
|
13
|
+
ArticleFacotoryにて下記のように記述することで、Articleインスタンスを作成する際にリレーションするUserインスタンスも生成、つまり、articlesに関連するユーザーのテスト用データが自動的に作られると思っていたのですが間違っているのでしょうか?
|
14
14
|
|
15
15
|
```
|
16
16
|
|
2
質問の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,6 +8,20 @@
|
|
8
8
|
|
9
9
|
|
10
10
|
|
11
|
+
エラー文を読むと、userのデータがないと言われているような気がしますが、私としては、
|
12
|
+
|
13
|
+
ArticleFacotoryにて、下記のように記述することで、Articleインスタンスを作成する際に、リレーションするUserインスタンスも生成,つまり、articlesに関連するユーザーのテスト用データが自動的に作られると思っていたのですが間違っているのでしょうか?
|
14
|
+
|
15
|
+
```
|
16
|
+
|
17
|
+
'user_id' => function () {
|
18
|
+
|
19
|
+
return factory(User::class)->create()->id;
|
20
|
+
|
21
|
+
```
|
22
|
+
|
23
|
+
|
24
|
+
|
11
25
|
開発中のアプリケーション上では、editメソッドを使用した機能はエラーなく実行できており、Userに関するエラーもchromeの検証機能、laravelのデバッグツールで確認したところ発生していません。
|
12
26
|
|
13
27
|
|
1
質問の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -418,6 +418,8 @@
|
|
418
418
|
|
419
419
|
|
420
420
|
|
421
|
+
```
|
422
|
+
|
421
423
|
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
422
424
|
|
423
425
|
`user_id` bigint(20) unsigned NOT NULL,
|
@@ -430,7 +432,7 @@
|
|
430
432
|
|
431
433
|
CONSTRAINT `articles_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
|
432
434
|
|
433
|
-
|
435
|
+
```
|
434
436
|
|
435
437
|
|
436
438
|
|
@@ -446,6 +448,8 @@
|
|
446
448
|
|
447
449
|
|
448
450
|
|
451
|
+
```
|
452
|
+
|
449
453
|
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
450
454
|
|
451
455
|
`name` varchar(255) NOT NULL,
|
@@ -468,6 +472,8 @@
|
|
468
472
|
|
469
473
|
UNIQUE KEY `users_email_unique` (`email`)
|
470
474
|
|
475
|
+
```
|
476
|
+
|
471
477
|
|
472
478
|
|
473
479
|
###news_link table
|
@@ -480,6 +486,8 @@
|
|
480
486
|
|
481
487
|
|
482
488
|
|
489
|
+
```
|
490
|
+
|
483
491
|
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
484
492
|
|
485
493
|
`article_id` bigint(20) unsigned NOT NULL,
|
@@ -493,3 +501,5 @@
|
|
493
501
|
KEY `news_links_article_id_foreign` (`article_id`),
|
494
502
|
|
495
503
|
CONSTRAINT `news_links_article_id_foreign` FOREIGN KEY (`article_id`) REFERENCES `articles` (`id`) ON DELETE CASCADE
|
504
|
+
|
505
|
+
```
|