質問編集履歴

5

htmlベース(?)の画像を追加しました。

2020/04/14 14:32

投稿

fork_
fork_

スコア43

test CHANGED
File without changes
test CHANGED
@@ -19,6 +19,10 @@
19
19
 
20
20
 
21
21
  ![イメージ説明](8d35c6169fce056bcc6a232f277e43ff.png)
22
+
23
+
24
+
25
+ ![イメージ説明](d7bd14bdcd6db024268bf6dc517f8512.png)
22
26
 
23
27
 
24
28
 

4

コントローラー側のコード追加しました。

2020/04/14 14:32

投稿

fork_
fork_

スコア43

test CHANGED
File without changes
test CHANGED
@@ -45,3 +45,151 @@
45
45
  @endif
46
46
 
47
47
  ```
48
+
49
+
50
+
51
+ コントローラーのコードは、こちらになります。
52
+
53
+
54
+
55
+ ```
56
+
57
+ <?php
58
+
59
+
60
+
61
+ namespace App\Http\Controllers;
62
+
63
+ use App\Post;
64
+
65
+ use Storage;
66
+
67
+ use Illuminate\Http\Request;
68
+
69
+ use Illuminate\Support\Facades\Auth;
70
+
71
+
72
+
73
+ class PostsController extends Controller
74
+
75
+ {
76
+
77
+ public function index()
78
+
79
+ {
80
+
81
+ //ログインしているユーザー情報を取得
82
+
83
+ $user = Auth::user();
84
+
85
+ $posts = Post::all();
86
+
87
+ return view('posts.index',['posts' => $posts,'user'=>$user]);
88
+
89
+ }
90
+
91
+
92
+
93
+ public function store(Request $request)
94
+
95
+ {
96
+
97
+ $request->validate([
98
+
99
+ 'text' => 'required',
100
+
101
+ ]);
102
+
103
+
104
+
105
+ // モデルからインスタンスを生成
106
+
107
+ $post = new Post;
108
+
109
+ // postsテーブルのtextに、フォームから送られたtextを保存する
110
+
111
+ $post->text = $request->text;
112
+
113
+ // userテーブルのidを、postsテーブルのuser_idとして保存する
114
+
115
+ $post->user_id = $request->user()->id;
116
+
117
+ // フォームリクエストの中に、画像ファイルが含まれているかどうかを条件分岐
118
+
119
+ if($request->hasFile('image')){
120
+
121
+ //s3アップロード開始
122
+
123
+ $image = $request->file('image');
124
+
125
+ // バケットの`test`フォルダへアップロード
126
+
127
+ $path = Storage::disk('s3')->putFile('test', $image, 'public');
128
+
129
+ // アップロードした画像のフルパスを取得
130
+
131
+ $post->image_path = Storage::disk('s3')->url($path);
132
+
133
+ }
134
+
135
+ // 保存
136
+
137
+ $post->save();
138
+
139
+ // 保存後 一覧ページへリダイレクト
140
+
141
+ return redirect('/');
142
+
143
+
144
+
145
+ }
146
+
147
+
148
+
149
+ public function update(Request $request, $id)
150
+
151
+ {
152
+
153
+ // idを元にレコードを検索して$postに代入
154
+
155
+ $post = Post::find($id);
156
+
157
+ // editで編集されたデータを$postにそれぞれ代入する
158
+
159
+ $post->text = $request->text;
160
+
161
+ // 保存
162
+
163
+ $post->save();
164
+
165
+ // 一覧ページへリダイレクト
166
+
167
+ return redirect('/');
168
+
169
+ }
170
+
171
+
172
+
173
+ public function destroy($id)
174
+
175
+ {
176
+
177
+ // idを元にレコードを検索
178
+
179
+ $post = Post::find($id);
180
+
181
+ // 削除
182
+
183
+ $post->delete();
184
+
185
+
186
+
187
+ // 一覧にリダイレクト
188
+
189
+ return redirect('/');
190
+
191
+ }
192
+
193
+ }
194
+
195
+ ```

3

アイコン部分のスクショです。

2020/04/14 14:08

投稿

fork_
fork_

スコア43

test CHANGED
File without changes
test CHANGED
@@ -15,6 +15,10 @@
15
15
 
16
16
 
17
17
  ![イメージ説明](fae29523fb1cc693d4bfd150d5a10c01.png)
18
+
19
+
20
+
21
+ ![イメージ説明](8d35c6169fce056bcc6a232f277e43ff.png)
18
22
 
19
23
 
20
24
 

2

バージョン修正しました。

2020/04/14 13:49

投稿

fork_
fork_

スコア43

test CHANGED
File without changes
test CHANGED
@@ -7,6 +7,12 @@
7
7
  一度、public/storageを削除し、php artisan storage:link として、再度シンボリックリンクを貼ってもうまくいきませんでした。
8
8
 
9
9
  他に、該当する箇所等ありますでしょうか??
10
+
11
+ laravelのバージョンは、下記になります。
12
+
13
+ Laravel Framework 5.8.37
14
+
15
+
10
16
 
11
17
  ![イメージ説明](fae29523fb1cc693d4bfd150d5a10c01.png)
12
18
 

1

コード追加しました。

2020/04/14 13:43

投稿

fork_
fork_

スコア43

test CHANGED
File without changes
test CHANGED
@@ -9,3 +9,29 @@
9
9
  他に、該当する箇所等ありますでしょうか??
10
10
 
11
11
  ![イメージ説明](fae29523fb1cc693d4bfd150d5a10c01.png)
12
+
13
+
14
+
15
+ 画像表示部分のコードはこちらになります。
16
+
17
+ ```
18
+
19
+ @auth
20
+
21
+ <!-- プロフィール画像が無かったら、デフォルトの画像を設定する -->
22
+
23
+ @if($user->profile_image == null)
24
+
25
+ <img src="/storage/noimage.png" style="width:50px; height:50px; border-radius:50%; position:relative; top: 79px;
26
+
27
+ left: 35px;">
28
+
29
+ @else
30
+
31
+ <img src="{{ $user->profile_image }}" style="width:50px; height:50px; border-radius:50%; position:relative; top: 79px;
32
+
33
+ left: 35px;">
34
+
35
+ @endif
36
+
37
+ ```