質問編集履歴

1

現状の追記を致しました。

2020/11/08 09:52

投稿

hina0823
hina0823

スコア15

test CHANGED
File without changes
test CHANGED
@@ -90,15 +90,177 @@
90
90
 
91
91
  ```
92
92
 
93
-
94
-
95
- 試したこと
93
+ 補足
96
-
94
+
97
- ---
95
+ ---
96
+
98
-
97
+ こちらの一覧表示画面ではユーザーのpostsテーブルの中身を取得できました。
98
+
99
- ddデバックを使$idと$postを調べたところ、$idはクリックしたユーザーのpostsテーブルのidが取得出来ていて、$postはnull返ってきています。
99
+ フォローしてユーザーのみを表示の画面でpostsテーブルの中身を取得する時に値がnullになって返ってきます。
100
+
100
-
101
+ dd(); デバックで確かめたところ、postsテーブルのidではなくfollowsテーブルのidを取得していることが原因なのですが、下記リレーションの記述でpostsテーブルのidを取得する記述が分からずにいます。
102
+
103
+
104
+
105
+ 該当ソースコード
106
+
107
+ ---
108
+
109
+ index.blade.php (一覧表示)
110
+
111
+ ```php
112
+
113
+ @foreach ($posts as $post)
114
+
115
+  <a href="{{ route('show', $post->id) }}" >{{ $post->username }}</a>
116
+
117
+ @endforeach
118
+
119
+ ```
120
+
121
+ FollowList.php
122
+
123
+ ```php
124
+
125
+ @foreach ($posts as $post)
126
+
127
+ <a href="{{ route('profile', $post->id) }}" >{{ $post->username }}</a>
128
+
129
+ @endforeach
130
+
131
+ ```
132
+
133
+
134
+
135
+ FollowsController.php (フォローしているユーザーの一覧表示)
136
+
137
+ ```php
138
+
139
+ public function followList()
140
+
141
+ {
142
+
143
+ $user = auth()->user();
144
+
145
+ $posts = \DB::table('users')
146
+
147
+ ->join('posts', 'posts.user_id', '=', 'users.id')
148
+
149
+ ->join('follows', 'follows.followed_id', '=', 'posts.user_id')
150
+
151
+ ->where('follows.following_id', '=', $user->id)
152
+
153
+ ->get();
154
+
155
+ return view('follows.followList',['posts' => $posts]);
156
+
157
+ }
158
+
159
+ ```
160
+
161
+
162
+
163
+ PostsController.php(クリックしたユーザーのデータを取得)
164
+
165
+ ```php
166
+
167
+ public function profile($id)
168
+
169
+ {
170
+
101
- $post = Post::find($id); この一文が原因だと推測はできるのでが…
171
+ $post = post::find($id);
172
+
173
+ return view('users.profile', ['post' => $post]);
174
+
175
+ }
176
+
177
+ ```
178
+
179
+ web.php
180
+
181
+ ```php
182
+
183
+ Route::get('/profile/{id}', 'PostsController@profile')->name('profile');
184
+
185
+ ```
186
+
187
+ followsテーブル
188
+
189
+ ```
190
+
191
+ +--------------+------------------+------+-----+-------------------+----------------+
192
+
193
+ | Field | Type | Null | Key | Default | Extra |
194
+
195
+ +--------------+------------------+------+-----+-------------------+----------------+
196
+
197
+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
198
+
199
+ | following_id | int(10) unsigned | NO | MUL | NULL | |
200
+
201
+ | followed_id | int(10) unsigned | NO | MUL | NULL | |
202
+
203
+ | created_at | timestamp | NO | | CURRENT_TIMESTAMP | |
204
+
205
+ +--------------+------------------+------+-----+-------------------+----------------+
206
+
207
+ ```
208
+
209
+
210
+
211
+ postsテーブル
212
+
213
+ ```
214
+
215
+ +------------+------------------+------+-----+---------+----------------+
216
+
217
+ | Field | Type | Null | Key | Default | Extra |
218
+
219
+ +------------+------------------+------+-----+---------+----------------+
220
+
221
+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
222
+
223
+ | user_id | int(10) unsigned | NO | MUL | NULL | |
224
+
225
+ | post | varchar(500) | NO | | NULL | |
226
+
227
+ | created_at | timestamp | YES | | NULL | |
228
+
229
+ | updated_at | timestamp | YES | | NULL | |
230
+
231
+ +------------+------------------+------+-----+---------+----------------+
232
+
233
+ ```
234
+
235
+ usersテーブル
236
+
237
+ ```
238
+
239
+ +------------+------------------+------+-----+----------+----------------+
240
+
241
+ | Field | Type | Null | Key | Default | Extra |
242
+
243
+ +------------+------------------+------+-----+----------+----------------+
244
+
245
+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
246
+
247
+ | username | varchar(255) | NO | | NULL | |
248
+
249
+ | mail | varchar(255) | NO | | NULL | |
250
+
251
+ | password | varchar(255) | NO | | NULL | |
252
+
253
+ | bio | varchar(400) | YES | | NULL | |
254
+
255
+ | images | varchar(255) | YES | | dawn.png | |
256
+
257
+ | created_at | timestamp | YES | | NULL | |
258
+
259
+ | updated_at | timestamp | YES | | NULL | |
260
+
261
+ +------------+------------------+------+-----+----------+----------------+
262
+
263
+ ```
102
264
 
103
265
  バージョン
104
266
 
@@ -112,4 +274,4 @@
112
274
 
113
275
 
114
276
 
115
- ご教授の程、宜しくお願い致します。
277
+ 長い質問になり申し訳ざいません。ご教授の程、宜しくお願い致します。