File tree Expand file tree Collapse file tree 5 files changed +165
-4
lines changed
Expand file tree Collapse file tree 5 files changed +165
-4
lines changed Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ public function __construct(PostService $post_service)
1616
1717 public function index ()
1818 {
19- $ data = $ this ->post ->get_posts_data ();
19+ $ data = $ this ->post ->getAverageRating ();
2020 dd ($ data );
2121 }
2222}
Original file line number Diff line number Diff line change 55
66class PostService
77{
8- public function get_posts_data (): array
8+ /**
9+ * 店舗ごとの平均評価値を取得する
10+ * @return array<int, object>
11+ */
12+ public function getAverageRating (): array
913 {
10- $ sql = 'SELECT * ' .PHP_EOL ;
14+ $ sql = 'SELECT shopname, AVG(rating) AS 平均値 ' .PHP_EOL ;
1115 $ sql .= 'FROM posts ' .PHP_EOL ;
16+ $ sql .= ' INNER JOIN ratings ' .PHP_EOL ;
17+ $ sql .= ' ON posts.id = ratings.post_id ' .PHP_EOL ;
18+ $ sql .= 'GROUP BY posts.id, posts.shopname ' .PHP_EOL ;
19+ $ sql .= 'ORDER BY 平均値 DESC ' .PHP_EOL ;
1220
13- return DB ::select ($ sql );
21+ return $ this ->formatAverageRatings (DB ::select ($ sql ));
22+ }
23+
24+ /**
25+ * 平均評価の値を小数第1位に丸める
26+ * @param array<int, object>
27+ * @return array<int, object>
28+ */
29+ private function formatAverageRatings (array $ results ): array
30+ {
31+ foreach ($ results as $ result ) {
32+ $ result ->平均値 = round ($ result ->平均値 , 1 );
33+ }
34+
35+ return $ results ;
1436 }
1537}
Original file line number Diff line number Diff line change 33use Illuminate \Database \Seeder ;
44use Database \Seeders \PostSeeder ;
55use Database \Seeders \TagSeeder ;
6+ use Database \Seeders \UserSeeder ;
67
78class DatabaseSeeder extends Seeder
89{
@@ -15,5 +16,6 @@ public function run()
1516 {
1617 $ this ->call (PostSeeder::class);
1718 $ this ->call (TagSeeder::class);
19+ $ this ->call (UserSeeder::class);
1820 }
1921}
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Database \Seeders ;
4+
5+ use Illuminate \Database \Console \Seeds \WithoutModelEvents ;
6+ use Illuminate \Support \Facades \DB ;
7+ use Illuminate \Database \Seeder ;
8+
9+ class RatingSeeder extends Seeder
10+ {
11+ /**
12+ * Run the database seeds.
13+ */
14+ public function run (): void
15+ {
16+ DB ::table ('ratings ' )->insert ([
17+ [
18+ 'user_id ' => 1 ,
19+ 'post_id ' => 2 ,
20+ 'rating ' => 3.0 ,
21+ 'created_at ' => now (),
22+ 'updated_at ' => now (),
23+ ],
24+ [
25+ 'user_id ' => 1 ,
26+ 'post_id ' => 3 ,
27+ 'rating ' => 4.0 ,
28+ 'created_at ' => now (),
29+ 'updated_at ' => now (),
30+ ],
31+ [
32+ 'user_id ' => 1 ,
33+ 'post_id ' => 4 ,
34+ 'rating ' => 5.0 ,
35+ 'created_at ' => now (),
36+ 'updated_at ' => now (),
37+ ],
38+ [
39+ 'user_id ' => 1 ,
40+ 'post_id ' => 5 ,
41+ 'rating ' => 1.0 ,
42+ 'created_at ' => now (),
43+ 'updated_at ' => now (),
44+ ],
45+ [
46+ 'user_id ' => 1 ,
47+ 'post_id ' => 6 ,
48+ 'rating ' => 1.0 ,
49+ 'created_at ' => now (),
50+ 'updated_at ' => now (),
51+ ],
52+ [
53+ 'user_id ' => 2 ,
54+ 'post_id ' => 7 ,
55+ 'rating ' => 1.5 ,
56+ 'created_at ' => now (),
57+ 'updated_at ' => now (),
58+ ],
59+ [
60+ 'user_id ' => 2 ,
61+ 'post_id ' => 6 ,
62+ 'rating ' => 3.6 ,
63+ 'created_at ' => now (),
64+ 'updated_at ' => now (),
65+ ],
66+ [
67+ 'user_id ' => 2 ,
68+ 'post_id ' => 5 ,
69+ 'rating ' => 2.5 ,
70+ 'created_at ' => now (),
71+ 'updated_at ' => now (),
72+ ],
73+ [
74+ 'user_id ' => 3 ,
75+ 'post_id ' => 4 ,
76+ 'rating ' => 5.0 ,
77+ 'created_at ' => now (),
78+ 'updated_at ' => now (),
79+ ],
80+ [
81+ 'user_id ' => 3 ,
82+ 'post_id ' => 3 ,
83+ 'rating ' => 2.1 ,
84+ 'created_at ' => now (),
85+ 'updated_at ' => now (),
86+ ],
87+ [
88+ 'user_id ' => 3 ,
89+ 'post_id ' => 2 ,
90+ 'rating ' => 3.4 ,
91+ 'created_at ' => now (),
92+ 'updated_at ' => now (),
93+ ],
94+ ]);
95+ }
96+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Database \Seeders ;
4+
5+ use Illuminate \Database \Console \Seeds \WithoutModelEvents ;
6+ use Illuminate \Support \Facades \DB ;
7+ use Illuminate \Database \Seeder ;
8+ use Illuminate \Support \Facades \Hash ;
9+
10+ class UserSeeder extends Seeder
11+ {
12+ /**
13+ * Run the database seeds.
14+ */
15+ public function run (): void
16+ {
17+ DB ::table ('users ' )->insert ([
18+ [
19+ 'name ' => '最初のユーザ ' ,
20+ 'email ' => 'aaa@aaa.com ' ,
21+ 'password ' => Hash::make ('password123 ' ),
22+ 'created_at ' => now (),
23+ 'updated_at ' => now (),
24+ ],
25+ [
26+ 'name ' => '二番目のユーザ ' ,
27+ 'email ' => 'bbb@bbb.com ' ,
28+ 'password ' => Hash::make ('password456 ' ),
29+ 'created_at ' => now (),
30+ 'updated_at ' => now (),
31+ ],
32+ [
33+ 'name ' => '三番目のユーザ ' ,
34+ 'email ' => 'ccc@ccc.com ' ,
35+ 'password ' => Hash::make ('password789 ' ),
36+ 'created_at ' => now (),
37+ 'updated_at ' => now (),
38+ ],
39+ ]);
40+ }
41+ }
You can’t perform that action at this time.
0 commit comments