File tree Expand file tree Collapse file tree 3 files changed +30
-7
lines changed
Expand file tree Collapse file tree 3 files changed +30
-7
lines changed Original file line number Diff line number Diff line change @@ -16,8 +16,16 @@ public function __construct(PostService $post_service)
1616
1717 public function index ()
1818 {
19- $ posts = $ this ->post ->getShopInfoWithTags ();
20- // dd($posts);
19+ $ id = 1 ;
20+ $ posts = [];
21+
22+ $ shopDetails = $ this ->post ->getShopDetails ();
23+ $ Favorites = $ this ->post ->getFavorites ($ id );
24+
25+ $ posts = [
26+ 'shopDetails ' => $ shopDetails ,
27+ 'Favorites ' => $ Favorites
28+ ];
2129
2230 return View ('posts.index ' , ['posts ' => $ posts ]);
2331 }
Original file line number Diff line number Diff line change 55
66class PostService
77{
8+
89 /**
9- * 店舗情報にタグ名を追加する
10+ * 店舗情報を取得する
1011 * @return array<int, object>
1112 */
12- public function getShopInfoWithTags (): array
13+ public function getShopDetails (): array
1314 {
1415 // 店舗情報を取得する
1516 $ shopInfo = $ this ->getShopInfo ();
16-
17+
1718 // タグを全種類取得する
1819 $ tags = $ this ->getAllTags ();
1920
@@ -37,7 +38,7 @@ public function getShopInfoWithTags(): array
3738 }
3839
3940 /**
40- * 店舗情報を取得する
41+ * 店舗情報をDBから取得する
4142 * @return array<int, object>
4243 */
4344 private function getShopInfo (): array
@@ -66,4 +67,18 @@ private function getAllTags(): array
6667
6768 return DB ::select ($ sql );
6869 }
70+
71+ /**
72+ * ユーザーがお気に入り(いいね)した投稿の post_id を取得する
73+ * @param int $user_id
74+ * @return array ユーザーが良いねした投稿のidリスト
75+ */
76+ public function getFavorites (int $ user_id ): array
77+ {
78+ $ sql = 'SELECT Favorites.post_id ' .PHP_EOL ;
79+ $ sql .= ' FROM Favorites ' .PHP_EOL ;
80+ $ sql .= 'WHERE user_id = :user_id ' .PHP_EOL ;
81+
82+ return DB ::select ($ sql , ['user_id ' => $ user_id ]);
83+ }
6984}
Original file line number Diff line number Diff line change 4545<body >
4646 <div class =" container" >
4747 <h1 >投稿一覧</h1 >
48- @foreach ($posts as $post )
48+ @foreach ($posts [ ' shopDetails ' ] as $post )
4949 <div class =" post" >
5050 @if ($post -> image_url )
5151 <img src =" {{ $post -> image_url } }" alt =" 画像" >
You can’t perform that action at this time.
0 commit comments