質問編集履歴

3

修正

2021/02/18 08:25

投稿

Airi.
Airi.

スコア8

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,9 @@
2
2
 
3
3
 
4
4
 
5
- *** 解決済 解決した内容はページ最後に追記します。(2020/2/18)**
5
+ *** 解決済 * 解決した内容はページ最後に追記します。(2020/2/18)**
6
+
7
+ ※ 回答者様のアドバイスを受けて、DBのimageカラムへは画像のファイル名ではなく自動で生成される文字列で表示する方法を取ることにしました。
6
8
 
7
9
 
8
10
 
@@ -420,9 +422,9 @@
420
422
 
421
423
  {
422
424
 
423
- $file_name = $data['image']->getClientOriginalName();
425
+
424
-
426
+
425
- $path = $data['image']->storeAs('public/image', $file_name);
427
+ $path = $data['image']->store('public/image');
426
428
 
427
429
 
428
430
 
@@ -432,7 +434,11 @@
432
434
 
433
435
  'profile' => $data['profile'],
434
436
 
435
- 'image' => basename($path),
437
+ 'image' => $path,
438
+
439
+ // 'image' => basename($path),←ファイル名を保存すると名前がかぶった時に上書きされてしまう。
440
+
441
+ // 'image' => $data['image'], ←'tmp_name'が保存されてしまう。
436
442
 
437
443
  'email' => $data['email'],
438
444
 
@@ -491,3 +497,33 @@
491
497
  <!-- 省略 -->
492
498
 
493
499
  ```
500
+
501
+ > 4. view/user/show.blade.php
502
+
503
+
504
+
505
+ 表示させる部分 <img src=**"{{Storage::url($user->image)}}"** alt="画像だよ" class="img-icon">
506
+
507
+ ```
508
+
509
+ <tr class="row">
510
+
511
+ <th class="col-4">icon</th>
512
+
513
+ <td class="col-6">
514
+
515
+ <div class="box-img">
516
+
517
+ <img src="{{Storage::url($user->image)}}" alt="画像だよ" class="img-icon">
518
+
519
+ {{-- <!-- <img src="../storage/image/{{$user->image}}" alt="画像だよ" class="img-icon"> --> --}}
520
+
521
+ {{-- <!-- <img src="/storage/{{$user->image}}" alt="画像だよ" class="img-icon"> -->--}}
522
+
523
+ </div>
524
+
525
+ </td>
526
+
527
+ </tr>
528
+
529
+ ```

2

解決した内容を目立たせた

2021/02/18 08:25

投稿

Airi.
Airi.

スコア8

test CHANGED
File without changes
test CHANGED
@@ -406,7 +406,7 @@
406
406
 
407
407
 
408
408
 
409
- ## ★ 解決できた記述(変更箇所のみ)
409
+ ## ★★★ 解決できた記述(変更箇所のみ)
410
410
 
411
411
 
412
412
 

1

解決方法を記述しました。

2021/02/18 07:49

投稿

Airi.
Airi.

スコア8

test CHANGED
File without changes
test CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
 
4
4
 
5
+ *** 解決済 解決した内容はページ最後に追記します。(2020/2/18)**
6
+
7
+
8
+
5
9
  ###* コード
6
10
 
7
11
 
@@ -320,11 +324,11 @@
320
324
 
321
325
  <div class="form-group row">
322
326
 
323
- <label for="my-icon" class="col-md-4 col-form-label text-md-right">Example file input</label>
327
+ <label for="image" class="col-md-4 col-form-label text-md-right">Example file input</label>
324
328
 
325
329
  <div class="col-md-6">
326
330
 
327
- <input type="file" class="form-control-file" id="my-icon" name="my-icon" accept="image/png, image/jpeg">
331
+ <input type="file" class="form-control-file" id="image" name="image" accept="image/png, image/jpeg">
328
332
 
329
333
  </div>
330
334
 
@@ -399,3 +403,91 @@
399
403
  **どのような手順で問題を解決すれば良いかなどアドバイスや、回答をいただけますと幸いです。**
400
404
 
401
405
  **何卒よろしくお願いいたします。**
406
+
407
+
408
+
409
+ ## ★ 解決できた記述(変更箇所のみ)
410
+
411
+
412
+
413
+ > 1. RegisterController.phpの記述
414
+
415
+
416
+
417
+ ```
418
+
419
+ protected function create(array $data)
420
+
421
+ {
422
+
423
+ $file_name = $data['image']->getClientOriginalName();
424
+
425
+ $path = $data['image']->storeAs('public/image', $file_name);
426
+
427
+
428
+
429
+ $user = User::create([
430
+
431
+ 'name' => $data['name'],
432
+
433
+ 'profile' => $data['profile'],
434
+
435
+ 'image' => basename($path),
436
+
437
+ 'email' => $data['email'],
438
+
439
+ 'password' => Hash::make($data['password']),
440
+
441
+ ]);
442
+
443
+
444
+
445
+ return $user;
446
+
447
+ }
448
+
449
+ ```
450
+
451
+ > 2. User.phpの記述
452
+
453
+
454
+
455
+ 変更なし
456
+
457
+
458
+
459
+ > 3. auth/register.blade.php
460
+
461
+
462
+
463
+ ```
464
+
465
+ <!-- my-iconを全てimageへ変更(カラム名と同じ) -->
466
+
467
+ <!-- 省略 -->
468
+
469
+ <form method="POST" action="{{ route('register') }}" enctype="multipart/form-data">
470
+
471
+ @csrf
472
+
473
+ <!-- 省略 -->
474
+
475
+ <div class="form-group row">
476
+
477
+ <label for="image" class="col-md-4 col-form-label text-md-right">Example file input</label>
478
+
479
+ <div class="col-md-6">
480
+
481
+ <input type="file" class="form-control-file" id="image" name="image" accept="image/png, image/jpeg">
482
+
483
+ </div>
484
+
485
+ </div>
486
+
487
+ <!-- 省略 -->
488
+
489
+ </form>
490
+
491
+ <!-- 省略 -->
492
+
493
+ ```