Skip to content

Commit f190b9d

Browse files
committed
画像の表示方法と修正
1 parent 2cf9371 commit f190b9d

File tree

3 files changed

+46
-18
lines changed

3 files changed

+46
-18
lines changed

app/Http/Controllers/PostController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ public function index(Request $request)
2626
$favorites = $this->post->getFavorites($this->userId);
2727
$pagerInfo = $this->post->getPagerInfo();
2828

29-
$posts = [
30-
'shopData' => $shopData,
29+
$data = [
30+
'shopData' => $shopData,
3131
'favorites' => $favorites,
32-
'pagerInfo' => $pagerInfo,
32+
'pagerInfo' => $pagerInfo
3333
];
3434

35-
return View('posts.index', ['posts' => $posts]);
35+
return View('posts.index', $data);
3636
}
3737

3838
public function create()

app/Services/PostService.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ public function convShopData(): array
3535
$tags = $this->getAllTags();
3636

3737
// View用にタグを変換
38-
foreach ($shopInfo as $si) {
39-
$si->tags = $this->convTagsData($si->tags, $tags);
38+
foreach ($shopInfo as &$si) {
39+
$si['tags'] = $this->convTagsData($si['tags'], $tags);
4040
}
4141

42+
unset($si);
43+
4244
return $shopInfo;
4345
}
4446

@@ -93,14 +95,17 @@ public function getShopInfo(): array
9395
$sql = 'SELECT posts.id, posts.shopname, posts.tags, deleted_flg,'.PHP_EOL;
9496
$sql .= 'ROUND(AVG(rating)::numeric, 1) AS avg_rating, MAX(image_url) AS image_url'.PHP_EOL;
9597
$sql .= 'FROM posts'.PHP_EOL;
96-
$sql .= ' INNER JOIN ratings'.PHP_EOL;
98+
$sql .= ' LEFT JOIN ratings'.PHP_EOL;
9799
$sql .= ' ON posts.id = ratings.post_id'.PHP_EOL;
98-
$sql .= ' INNER JOIN images'.PHP_EOL;
100+
$sql .= ' LEFT JOIN images'.PHP_EOL;
99101
$sql .= ' ON posts.id = images.post_id'.PHP_EOL;
100102
$sql .= 'GROUP BY posts.id, posts.shopname, posts.tags, posts.deleted_flg'.PHP_EOL;
101103
$sql .= 'ORDER BY avg_rating DESC'.PHP_EOL;
102104

103-
return $this->pager(DB::select($sql));
105+
$result = DB::select($sql);
106+
$convArray = array_map(fn($row) => (array) $row, $result); // 配列内のオブジェクトを配列に変換
107+
108+
return $this->pager($convArray);
104109
}
105110

106111
/**

resources/views/posts/index.blade.php

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@
3838
margin: 0 auto;
3939
}
4040
41+
.create-shop-content {
42+
text-align: center;
43+
margin-bottom: 20px;
44+
}
45+
46+
.create-shop-content a {
47+
display: inline-block;
48+
background-color: #28a745;
49+
color: white;
50+
padding: 10px 20px;
51+
border-radius: 5px;
52+
text-decoration: none;
53+
font-weight: bold;
54+
transition: background-color 0.3s;
55+
}
56+
4157
.post {
4258
display: flex;
4359
align-items: center;
@@ -120,19 +136,26 @@
120136
<body>
121137
<div class="container">
122138
<h1>投稿一覧</h1>
123-
@foreach ($posts['shopData'] as $post)
139+
<div class="create-shop-content">
140+
<a href="{{ route('create') }}">
141+
+ 新しい店舗を追加
142+
</a>
143+
</div>
144+
@foreach ($shopData as $post)
124145
<div class="post">
125-
@if($post->image_url)
126-
<img src="{{ $post->image_url }}" alt="画像">
146+
@if(!is_null($post['image_url']))
147+
<div><img src="{{ $post['image_url'] }}" alt="画像"></div>
148+
@elseif(is_null($post['image_url']))
149+
<div><img src="https://picsum.photos/200/300" alt="画像"></div>
127150
@endif
128151
<div class="post-content">
129-
<h2><a href="{{ route('show', ['id' => $post->id]) }}">{{ $post->shopname }}</a></h2>
130-
<p>評価: <strong>{{ number_format($post->avg_rating, 1) }}</strong> ⭐</p>
152+
<h2><a href="{{ route('show', ['id' => $post['id']]) }}">{{ $post['shopname'] }}</a></h2>
153+
<p>評価: <strong>{{ number_format($post['avg_rating'], 1) }}</strong> ⭐</p>
131154

132155
<!-- タグ表示 -->
133-
@if (!empty($post->tags))
156+
@if (!empty($post['tags']))
134157
<div class="tags">
135-
@foreach ($post->tags as $tag)
158+
@foreach ($post['tags'] as $tag)
136159
<span class="tag">{{ $tag['name'] }}</span>
137160
@endforeach
138161
</div>
@@ -146,8 +169,8 @@
146169

147170
<!-- ページネーション -->
148171
<div class="pagination">
149-
@for ($i = 1; $i <= $posts['pagerInfo']['maxPage']; $i++)
150-
@if ($i == $posts['pagerInfo']['nowPage'])
172+
@for ($i = 1; $i <= $pagerInfo['maxPage']; $i++)
173+
@if ($i == $pagerInfo['nowPage'])
151174
<span class="active">{{ $i }}</span>
152175
@else
153176
<a href="?page_id={{ $i }}">{{ $i }}</a>

0 commit comments

Comments
 (0)