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

質問編集履歴

1

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

2020/11/08 09:52

投稿

hina0823
hina0823

スコア15

title CHANGED
File without changes
body CHANGED
@@ -44,15 +44,96 @@
44
44
  | updated_at | timestamp | YES | | NULL | |
45
45
  +------------+------------------+------+-----+---------+----------------+
46
46
  ```
47
+ 補足
48
+ ---
49
+ こちらの一覧表示画面ではユーザーのpostsテーブルの中身を取得できました。
50
+ フォローしているユーザーのみを表示の画面でpostsテーブルの中身を取得する時に値がnullになって返ってきます。
51
+ dd(); デバックで確かめたところ、postsテーブルのidではなくfollowsテーブルのidを取得していることが原因なのですが、下記リレーションの記述でpostsテーブルのidを取得する記述が分からずにいます。
47
52
 
48
- 試したこと
53
+ 該当ソースコード
49
54
  ---
55
+ index.blade.php (一覧表示)
56
+ ```php
57
+ @foreach ($posts as $post)
58
+  <a href="{{ route('show', $post->id) }}" >{{ $post->username }}</a>
59
+ @endforeach
60
+ ```
61
+ FollowList.php
62
+ ```php
63
+ @foreach ($posts as $post)
64
+ <a href="{{ route('profile', $post->id) }}" >{{ $post->username }}</a>
65
+ @endforeach
66
+ ```
67
+
68
+ FollowsController.php (フォローしているユーザーの一覧表示)
69
+ ```php
70
+ public function followList()
71
+ {
72
+ $user = auth()->user();
73
+ $posts = \DB::table('users')
74
+ ->join('posts', 'posts.user_id', '=', 'users.id')
75
+ ->join('follows', 'follows.followed_id', '=', 'posts.user_id')
76
+ ->where('follows.following_id', '=', $user->id)
77
+ ->get();
50
- ddデバックを使い$idと$postを調べたところ、$idはクリックしたユーザーのpostsテーブルのidが取得出来ていて、$postはnullで返ってきています。
78
+ return view('follows.followList',['posts' => $posts]);
79
+ }
80
+ ```
81
+
82
+ PostsController.php(クリックしたユーザーのデータを取得)
83
+ ```php
84
+ public function profile($id)
85
+ {
51
- $post = Post::find($id); この一文が原因だと推測はできるのでが…
86
+ $post = post::find($id);
87
+ return view('users.profile', ['post' => $post]);
88
+ }
89
+ ```
90
+ web.php
91
+ ```php
92
+ Route::get('/profile/{id}', 'PostsController@profile')->name('profile');
93
+ ```
94
+ followsテーブル
95
+ ```
96
+ +--------------+------------------+------+-----+-------------------+----------------+
97
+ | Field | Type | Null | Key | Default | Extra |
98
+ +--------------+------------------+------+-----+-------------------+----------------+
99
+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
100
+ | following_id | int(10) unsigned | NO | MUL | NULL | |
101
+ | followed_id | int(10) unsigned | NO | MUL | NULL | |
102
+ | created_at | timestamp | NO | | CURRENT_TIMESTAMP | |
103
+ +--------------+------------------+------+-----+-------------------+----------------+
104
+ ```
105
+
106
+ postsテーブル
107
+ ```
108
+ +------------+------------------+------+-----+---------+----------------+
109
+ | Field | Type | Null | Key | Default | Extra |
110
+ +------------+------------------+------+-----+---------+----------------+
111
+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
112
+ | user_id | int(10) unsigned | NO | MUL | NULL | |
113
+ | post | varchar(500) | NO | | NULL | |
114
+ | created_at | timestamp | YES | | NULL | |
115
+ | updated_at | timestamp | YES | | NULL | |
116
+ +------------+------------------+------+-----+---------+----------------+
117
+ ```
118
+ usersテーブル
119
+ ```
120
+ +------------+------------------+------+-----+----------+----------------+
121
+ | Field | Type | Null | Key | Default | Extra |
122
+ +------------+------------------+------+-----+----------+----------------+
123
+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
124
+ | username | varchar(255) | NO | | NULL | |
125
+ | mail | varchar(255) | NO | | NULL | |
126
+ | password | varchar(255) | NO | | NULL | |
127
+ | bio | varchar(400) | YES | | NULL | |
128
+ | images | varchar(255) | YES | | dawn.png | |
129
+ | created_at | timestamp | YES | | NULL | |
130
+ | updated_at | timestamp | YES | | NULL | |
131
+ +------------+------------------+------+-----+----------+----------------+
132
+ ```
52
133
  バージョン
53
134
  ---
54
135
  laravel 5.5.48
55
136
  php 7.4.5
56
137
  homestead
57
138
 
58
- ご教授の程、宜しくお願い致します。
139
+ 長い質問になり申し訳ざいません。ご教授の程、宜しくお願い致します。