Skip to content

Commit 96f6a15

Browse files
committed
良いねをDBから取得する処理
1 parent ea05e40 commit 96f6a15

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

app/Http/Controllers/PostController.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff 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
}

app/Services/PostService.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55

66
class 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
}

resources/views/posts/index.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
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="画像">

0 commit comments

Comments
 (0)