teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

6

`$request->hasFile(...)` というチェックを追加しました。

2021/02/11 07:31

投稿

Lulucom
Lulucom

スコア1904

answer CHANGED
@@ -18,7 +18,8 @@
18
18
 
19
19
  $stock_cosmetic->save();
20
20
 
21
- if ($request->file('image')->isValid()) { // ファイルが存在し、アップロードに問題がなかった場合
21
+ if ($request->hasFile('image') && $request->file('image')->isValid()) {
22
+ // ファイルが存在し、アップロードに問題がなかった場合
22
23
  $path = $request->image->storePubliclyAs('/task', $stock_cosmetic->id . '.jpg', ['disk' => 's3']);
23
24
  $stock_cosmetic->image = $path;
24
25
  $stock_cosmetic->save();

5

StockController@storeを修正しました。

2021/02/11 07:31

投稿

Lulucom
Lulucom

スコア1904

answer CHANGED
@@ -15,12 +15,14 @@
15
15
 
16
16
  $stock_cosmetic->color = $request->input('product');
17
17
  $stock_cosmetic->user_id = Auth::id();
18
+
19
+ $stock_cosmetic->save();
20
+
18
21
  if ($request->file('image')->isValid()) { // ファイルが存在し、アップロードに問題がなかった場合
19
22
  $path = $request->image->storePubliclyAs('/task', $stock_cosmetic->id . '.jpg', ['disk' => 's3']);
20
23
  $stock_cosmetic->image = $path;
24
+ $stock_cosmetic->save();
21
25
  }
22
-
23
- $stock_cosmetic->save();
24
26
  ```
25
27
 
26
28
  参考: [ファイル](https://readouble.com/laravel/8.x/ja/requests.html#files)

4

StockController@storeのコード例を修正し、参考リンクも追記しました。

2021/02/11 06:59

投稿

Lulucom
Lulucom

スコア1904

answer CHANGED
@@ -15,8 +15,12 @@
15
15
 
16
16
  $stock_cosmetic->color = $request->input('product');
17
17
  $stock_cosmetic->user_id = Auth::id();
18
+ if ($request->file('image')->isValid()) { // ファイルが存在し、アップロードに問題がなかった場合
18
- $path = $request->image->storePubliclyAs('/task', $stock_cosmetic->id . '.jpg', ['disk' => 's3']);
19
+ $path = $request->image->storePubliclyAs('/task', $stock_cosmetic->id . '.jpg', ['disk' => 's3']);
19
- $stock_cosmetic->image = $path;
20
+ $stock_cosmetic->image = $path;
21
+ }
20
22
 
21
23
  $stock_cosmetic->save();
22
- ```
24
+ ```
25
+
26
+ 参考: [ファイル](https://readouble.com/laravel/8.x/ja/requests.html#files)

3

追記した文の軽微な改善

2021/02/11 05:03

投稿

Lulucom
Lulucom

スコア1904

answer CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  (追記)
10
10
 
11
- また、StockControllerクラスのstoreメソッドでも、S3でのパスをモデルに保存する必要ありそうです。
11
+ また、StockControllerクラスのstoreメソッドでも、S3でのパスをモデルのimageプロパティに保存する必要ありそうです。
12
12
 
13
13
  ```php
14
14
  $stock_cosmetic = new StockCosmetic();

2

StockController@storeの修正について追記しました。

2021/02/11 04:55

投稿

Lulucom
Lulucom

スコア1904

answer CHANGED
@@ -4,4 +4,19 @@
4
4
 
5
5
  ```php
6
6
  <img src="{{ Storage::disk('s3')->url("task/{$stock_cosmetic->id}.jpg") }}">
7
+ ```
8
+
9
+ (追記)
10
+
11
+ また、StockControllerクラスのstoreメソッドでも、S3でのパスをモデルに保存する必要もありそうです。
12
+
13
+ ```php
14
+ $stock_cosmetic = new StockCosmetic();
15
+
16
+ $stock_cosmetic->color = $request->input('product');
17
+ $stock_cosmetic->user_id = Auth::id();
18
+ $path = $request->image->storePubliclyAs('/task', $stock_cosmetic->id . '.jpg', ['disk' => 's3']);
19
+ $stock_cosmetic->image = $path;
20
+
21
+ $stock_cosmetic->save();
7
22
  ```

1

htmlにしてしまっていたのをphpに修正しました。

2021/02/11 04:54

投稿

Lulucom
Lulucom

スコア1904

answer CHANGED
@@ -2,6 +2,6 @@
2
2
 
3
3
  と書かれていますので、以下のようにする必要があるのではないでしょうか。
4
4
 
5
- ```html
5
+ ```php
6
6
  <img src="{{ Storage::disk('s3')->url("task/{$stock_cosmetic->id}.jpg") }}">
7
7
  ```