回答編集履歴
3
編集
answer
CHANGED
@@ -42,7 +42,7 @@
|
|
42
42
|
|
43
43
|
$teams = Team::with(['team_users'])->where('user_id', $user_id)->get();
|
44
44
|
$team_users = $teams->map(function($team){
|
45
|
-
return $team->team_users->user_id;
|
45
|
+
return $team->team_users->pluck('user_id');
|
46
46
|
});
|
47
47
|
|
48
48
|
dd($team_users);
|
2
追記
answer
CHANGED
@@ -29,4 +29,21 @@
|
|
29
29
|
->pluck('user_id'); //そのチームに所属しているuser_id取得
|
30
30
|
```
|
31
31
|
|
32
|
-
[https://readouble.com/laravel/7.x/ja/queries.html](https://readouble.com/laravel/7.x/ja/queries.html)
|
32
|
+
[https://readouble.com/laravel/7.x/ja/queries.html](https://readouble.com/laravel/7.x/ja/queries.html)
|
33
|
+
|
34
|
+
---
|
35
|
+
|
36
|
+
Laravelの便利な機能であるリレーションを十分に活かしていません。
|
37
|
+
|
38
|
+
↓改善したソース
|
39
|
+
|
40
|
+
```php
|
41
|
+
$user_id = Auth::id();
|
42
|
+
|
43
|
+
$teams = Team::with(['team_users'])->where('user_id', $user_id)->get();
|
44
|
+
$team_users = $teams->map(function($team){
|
45
|
+
return $team->team_users->user_id;
|
46
|
+
});
|
47
|
+
|
48
|
+
dd($team_users);
|
49
|
+
```
|
1
追記
answer
CHANGED
@@ -14,13 +14,6 @@
|
|
14
14
|
他人の貴重な時間をいただくんですから、質問文は丁寧に書くべきです。
|
15
15
|
インデントもぐちゃぐちゃなのはいただけないです。
|
16
16
|
|
17
|
-
(2) MVCの理解が足りていません。
|
18
|
-
|
19
|
-
`TeamUser` は Team と User を結ぶ「中間テーブルのはず。Laravel において中間テーブルはモデルにしません。
|
20
|
-
|
21
|
-
こちらの「多対多」の項目をしっかり読んでおきましょう
|
22
|
-
[https://readouble.com/laravel/7.x/ja/eloquent-relationships.html#many-to-many](https://readouble.com/laravel/7.x/ja/eloquent-relationships.html#many-to-many)
|
23
|
-
|
24
17
|
----
|
25
18
|
|
26
19
|
```diff
|