質問編集履歴

2

追加

2019/03/15 20:52

投稿

sk2938
sk2938

スコア21

test CHANGED
File without changes
test CHANGED
@@ -475,3 +475,73 @@
475
475
  省略
476
476
 
477
477
  ```
478
+
479
+ 2019_03_16_034561_create_user_tag_table.php
480
+
481
+ ```
482
+
483
+ <?php
484
+
485
+
486
+
487
+ use Illuminate\Support\Facades\Schema;
488
+
489
+ use Illuminate\Database\Schema\Blueprint;
490
+
491
+ use Illuminate\Database\Migrations\Migration;
492
+
493
+
494
+
495
+ class CreateUserTagTable extends Migration
496
+
497
+ {
498
+
499
+ /**
500
+
501
+ * Run the migrations.
502
+
503
+ *
504
+
505
+ * @return void
506
+
507
+ */
508
+
509
+ public function up()
510
+
511
+ {
512
+
513
+ Schema::create('user_tag', function (Blueprint $table) {
514
+
515
+ $table->increments('id');
516
+
517
+ $table->integer('user_id');
518
+
519
+ $table->integer('tag_id');
520
+
521
+ });
522
+
523
+ }
524
+
525
+
526
+
527
+ /**
528
+
529
+ * Reverse the migrations.
530
+
531
+ *
532
+
533
+ * @return void
534
+
535
+ */
536
+
537
+ public function down()
538
+
539
+ {
540
+
541
+ Schema::dropIfExists('user_tag');
542
+
543
+ }
544
+
545
+ }
546
+
547
+ ```

1

追加

2019/03/15 20:52

投稿

sk2938
sk2938

スコア21

test CHANGED
File without changes
test CHANGED
@@ -116,8 +116,6 @@
116
116
 
117
117
  ];
118
118
 
119
-
120
-
121
119
  public function articles()
122
120
 
123
121
  {
@@ -126,8 +124,6 @@
126
124
 
127
125
  }
128
126
 
129
-
130
-
131
127
  public function users()
132
128
 
133
129
  {
@@ -174,24 +170,18 @@
174
170
 
175
171
  ];
176
172
 
177
-
178
-
179
173
  protected $hidden = [
180
174
 
181
175
  'password', 'remember_token',
182
176
 
183
177
  ];
184
178
 
185
-
186
-
187
179
  protected $casts = [
188
180
 
189
181
  'email_verified_at' => 'datetime',
190
182
 
191
183
  ];
192
184
 
193
-
194
-
195
185
  public function tags()
196
186
 
197
187
  {
@@ -200,8 +190,6 @@
200
190
 
201
191
  }
202
192
 
203
-
204
-
205
193
  }
206
194
 
207
195
  ```
@@ -254,41 +242,167 @@
254
242
 
255
243
  */
256
244
 
257
-
258
-
259
245
  use RegistersUsers;
260
246
 
261
247
 
262
248
 
249
+ protected $redirectTo = '/home';
250
+
251
+
252
+
253
+ public function __construct()
254
+
255
+ {
256
+
257
+ $this->middleware('guest');
258
+
259
+ }
260
+
261
+
262
+
263
+ protected function validator(array $data)
264
+
265
+ {
266
+
267
+ return Validator::make($data, [
268
+
269
+ 'name' => ['required', 'string', 'max:255'],
270
+
271
+ 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
272
+
273
+ 'password' => ['required', 'string', 'min:8', 'confirmed'],
274
+
275
+ 'age' => ['required', 'numeric'],
276
+
277
+ 'gender' => ['required'],
278
+
279
+ 'post_code' => ['required', 'max:15'],
280
+
281
+ 'prefecture_code' => ['required', 'numeric'],
282
+
283
+ 'address' => ['required'],
284
+
285
+ ]);
286
+
287
+ }
288
+
289
+
290
+
291
+ protected function create(array $data)
292
+
293
+ {
294
+
295
+ return User::create([
296
+
297
+ 'name' => $data['name'],
298
+
299
+ 'email' => $data['email'],
300
+
301
+ 'password' => Hash::make($data['password']),
302
+
303
+ 'age' => $data['age'],
304
+
305
+ 'gender' => $data['gender'],
306
+
307
+ 'post_code' => $data['post_code'],
308
+
309
+ 'prefecture_code' => $data['prefecture_code'],
310
+
311
+ 'address' => $data['address'],
312
+
313
+ ]);
314
+
315
+ }
316
+
317
+ }
318
+
319
+ ```
320
+
321
+ RegistersUsers.php
322
+
323
+ ```
324
+
325
+ <?php
326
+
327
+
328
+
329
+ namespace Illuminate\Foundation\Auth;
330
+
331
+
332
+
333
+ use Illuminate\Http\Request;
334
+
335
+ use Illuminate\Support\Facades\Auth;
336
+
337
+ use Illuminate\Auth\Events\Registered;
338
+
339
+ use App\Tag;
340
+
341
+
342
+
343
+ trait RegistersUsers
344
+
345
+ {
346
+
347
+ use RedirectsUsers;
348
+
349
+
350
+
263
351
  /**
264
352
 
265
- * Where to redirect users after registration.
353
+ * Show the application registration form.
266
354
 
267
355
  *
268
356
 
269
- * @var string
357
+ * @return \Illuminate\Http\Response
270
358
 
271
359
  */
272
360
 
361
+ public function showRegistrationForm()
362
+
363
+ {
364
+
273
- protected $redirectTo = '/home';
365
+ $prefs = config('pref');
366
+
367
+ $tags = Tag::all();
368
+
369
+ return view('auth.register',['prefs' => $prefs, 'tags' => $tags]);
370
+
371
+ }
274
372
 
275
373
 
276
374
 
277
375
  /**
278
376
 
279
- * Create a new controller instance.
377
+ * Handle a registration request for the application.
280
378
 
281
379
  *
282
380
 
381
+ * @param \Illuminate\Http\Request $request
382
+
283
- * @return void
383
+ * @return \Illuminate\Http\Response
284
384
 
285
385
  */
286
386
 
287
- public function __construct()
387
+ public function register(Request $request)
288
-
388
+
289
- {
389
+ {
390
+
290
-
391
+ $this->validator($request->all())->validate();
392
+
393
+
394
+
395
+ event(new Registered($user = $this->create($request->all())));
396
+
397
+
398
+
291
- $this->middleware('guest');
399
+ $this->guard()->login($user);
400
+
401
+
402
+
403
+ return $this->registered($request, $user)
404
+
405
+ ?: redirect($this->redirectPath());
292
406
 
293
407
  }
294
408
 
@@ -296,39 +410,19 @@
296
410
 
297
411
  /**
298
412
 
299
- * Get a validator for an incoming registration request.
413
+ * Get the guard to be used during registration.
300
414
 
301
415
  *
302
416
 
303
- * @param array $data
304
-
305
- * @return \Illuminate\Contracts\Validation\Validator
417
+ * @return \Illuminate\Contracts\Auth\StatefulGuard
306
418
 
307
419
  */
308
420
 
309
- protected function validator(array $data)
421
+ protected function guard()
310
-
422
+
311
- {
423
+ {
312
-
424
+
313
- return Validator::make($data, [
425
+ return Auth::guard();
314
-
315
- 'name' => ['required', 'string', 'max:255'],
316
-
317
- 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
318
-
319
- 'password' => ['required', 'string', 'min:8', 'confirmed'],
320
-
321
- 'age' => ['required', 'numeric'],
322
-
323
- 'gender' => ['required'],
324
-
325
- 'post_code' => ['required', 'max:15'],
326
-
327
- 'prefecture_code' => ['required', 'numeric'],
328
-
329
- 'address' => ['required'],
330
-
331
- ]);
332
426
 
333
427
  }
334
428
 
@@ -336,39 +430,23 @@
336
430
 
337
431
  /**
338
432
 
339
- * Create a new user instance after a valid registration.
433
+ * The user has been registered.
340
434
 
341
435
  *
342
436
 
437
+ * @param \Illuminate\Http\Request $request
438
+
343
- * @param array $data
439
+ * @param mixed $user
344
-
440
+
345
- * @return \App\User
441
+ * @return mixed
346
442
 
347
443
  */
348
444
 
349
- protected function create(array $data)
445
+ protected function registered(Request $request, $user)
350
-
446
+
351
- {
447
+ {
352
-
353
- return User::create([
448
+
354
-
355
- 'name' => $data['name'],
356
-
357
- 'email' => $data['email'],
358
-
359
- 'password' => Hash::make($data['password']),
360
-
361
- 'age' => $data['age'],
449
+ //
362
-
363
- 'gender' => $data['gender'],
364
-
365
- 'post_code' => $data['post_code'],
366
-
367
- 'prefecture_code' => $data['prefecture_code'],
368
-
369
- 'address' => $data['address'],
370
-
371
- ]);
372
450
 
373
451
  }
374
452
 
@@ -376,138 +454,24 @@
376
454
 
377
455
  ```
378
456
 
379
- RegistersUsers.php
380
-
381
- ```
382
-
383
- <?php
384
-
385
-
386
-
387
- namespace Illuminate\Foundation\Auth;
388
-
389
-
390
-
391
- use Illuminate\Http\Request;
392
-
393
- use Illuminate\Support\Facades\Auth;
394
-
395
- use Illuminate\Auth\Events\Registered;
396
-
397
- use App\Tag;
398
-
399
-
400
-
401
- trait RegistersUsers
402
-
403
- {
404
-
405
- use RedirectsUsers;
406
-
407
-
408
-
409
- /**
410
-
411
- * Show the application registration form.
412
-
413
- *
414
-
415
- * @return \Illuminate\Http\Response
416
-
417
- */
418
-
419
- public function showRegistrationForm()
420
-
421
- {
422
-
423
- $prefs = config('pref');
424
-
425
- $tags = Tag::all();
426
-
427
- return view('auth.register',['prefs' => $prefs, 'tags' => $tags]);
428
-
429
- }
430
-
431
-
432
-
433
- /**
434
-
435
- * Handle a registration request for the application.
436
-
437
- *
438
-
439
- * @param \Illuminate\Http\Request $request
440
-
441
- * @return \Illuminate\Http\Response
442
-
443
- */
444
-
445
- public function register(Request $request)
446
-
447
- {
448
-
449
- $this->validator($request->all())->validate();
450
-
451
-
452
-
453
- event(new Registered($user = $this->create($request->all())));
454
-
455
-
456
-
457
- $this->guard()->login($user);
458
-
459
-
460
-
461
- return $this->registered($request, $user)
462
-
463
- ?: redirect($this->redirectPath());
464
-
465
- }
466
-
467
-
468
-
469
- /**
470
-
471
- * Get the guard to be used during registration.
472
-
473
- *
474
-
475
- * @return \Illuminate\Contracts\Auth\StatefulGuard
476
-
477
- */
478
-
479
- protected function guard()
480
-
481
- {
482
-
483
- return Auth::guard();
484
-
485
- }
486
-
487
-
488
-
489
- /**
490
-
491
- * The user has been registered.
492
-
493
- *
494
-
495
- * @param \Illuminate\Http\Request $request
496
-
497
- * @param mixed $user
498
-
499
- * @return mixed
500
-
501
- */
502
-
503
- protected function registered(Request $request, $user)
504
-
505
- {
506
-
507
- //
508
-
509
- }
510
-
511
- }
512
-
513
- ```
457
+ register.blade.php
458
+
459
+ ```
460
+
461
+ 省略
462
+
463
+ <form method="POST" action="{{ route('register') }}">
464
+
465
+ @csrf
466
+
467
+ #関連付けるタグ
468
+
469
+ @foreach ($tags as $tag)
470
+
471
+ <input type="checkbox" name="tags[]" value="{{ $tag->id }}">{{ $tag->name }}
472
+
473
+ @endforeach
474
+
475
+ 省略
476
+
477
+ ```