回答編集履歴
1
追記
answer
CHANGED
|
@@ -9,4 +9,46 @@
|
|
|
9
9
|
if 画像がある(画像のrequestがnullじゃない) -> 画像のアップロード
|
|
10
10
|
他をsave
|
|
11
11
|
```
|
|
12
|
-
みたいな感じにすると思います。
|
|
12
|
+
みたいな感じにすると思います。
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### コード
|
|
16
|
+
|
|
17
|
+
テスト全くしてないので、動かなかった場合は言ってください。
|
|
18
|
+
```PHP
|
|
19
|
+
|
|
20
|
+
public function update(Request $request, $id) {
|
|
21
|
+
$cat = Cat::find($id);
|
|
22
|
+
|
|
23
|
+
// 画像をリサイズ
|
|
24
|
+
$file = $request->file('mainimage_path');
|
|
25
|
+
|
|
26
|
+
if( !is_null( $file ) ) {
|
|
27
|
+
// 画像の拡張子を取得
|
|
28
|
+
$extension = $request->file('mainimage_path')->getClientOriginalExtension();
|
|
29
|
+
// 画像の名前を取得
|
|
30
|
+
$filename = $request->file('mainimage_path')->getClientOriginalName();
|
|
31
|
+
$timestamp = time();
|
|
32
|
+
$filename = $timestamp . $filename;
|
|
33
|
+
// 画像をリサイズ変更したところ
|
|
34
|
+
$width = 500;
|
|
35
|
+
$resize_img = Image::make($file)->resize($width, null, function($constraint){
|
|
36
|
+
$constraint->aspectRatio();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
$path = Storage::disk('s3')->put('/myprefix/'.$filename, $resize_img->stream()->__toString(), 'public');
|
|
40
|
+
// 画像のURLを参照
|
|
41
|
+
$url = Storage::disk('s3')->url('myprefix/'.$filename);
|
|
42
|
+
//dd($url);
|
|
43
|
+
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
$cat->mainimage_path = $url;
|
|
47
|
+
|
|
48
|
+
$cat->save();
|
|
49
|
+
|
|
50
|
+
return redirect()->action('CatsController@index');
|
|
51
|
+
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
これ以上はMigrationやバリデーションも欲しいです。
|