質問編集履歴
3
title
CHANGED
File without changes
|
body
CHANGED
@@ -43,9 +43,9 @@
|
|
43
43
|
```
|
44
44
|
**Category.php**
|
45
45
|
```
|
46
|
-
public function
|
46
|
+
public function products()
|
47
47
|
{
|
48
|
-
return $this->hasMany('App\
|
48
|
+
return $this->hasMany('App\Product');
|
49
49
|
}
|
50
50
|
```
|
51
51
|
**Favorite.php**
|
2
訂正
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,10 +1,96 @@
|
|
1
1
|
https://teratail.com/questions/50857?link=qa_related_pc
|
2
2
|
この記事を参考にして、中間テーブルのリレーションを取得したのですがさらにここからリレーション先のリレーションを取得するにはどうしたらいいのでしょう。
|
3
|
-
|
3
|
+
productsテーブルにcategory_idというカラムがありcategoriesテーブルとリレーションしているとします。参考記事の方法でproductsテーブルの情報を取得し、さらにそこからそのproductsテーブルのcategory_idに紐付いたcategoriesテーブルの情報を取得するにはどのようにすればいいのでしょうか。
|
4
|
-
|
4
|
+
■テーブル設計
|
5
|
+
【usersテーブル】
|
6
|
+
id | name | password | created_at | updated_at |
|
5
7
|
|
8
|
+
【productsテーブル】
|
9
|
+
id | title | price | category_id |user_id | created_at | updated_at |
|
10
|
+
|
11
|
+
【favoritesテーブル】
|
12
|
+
user_id | product_id | created_at | updated_at |
|
13
|
+
|
14
|
+
【categoriesテーブル】
|
15
|
+
id | name | created_at | updated_at |
|
16
|
+
|
17
|
+
|
18
|
+
**User.php**
|
6
19
|
```
|
20
|
+
public function products()
|
21
|
+
{
|
22
|
+
return $this->hasMany('App\Producdt');
|
23
|
+
}
|
24
|
+
public function favorites()
|
25
|
+
{
|
7
|
-
|
26
|
+
return $this->belongsToMany('App\Products', 'favorites');
|
27
|
+
}
|
8
28
|
```
|
29
|
+
**Product.php**
|
30
|
+
```
|
31
|
+
public function category()
|
32
|
+
{
|
33
|
+
return $this->belongsTo('App\Category');
|
34
|
+
}
|
35
|
+
public function favorites()
|
36
|
+
{
|
37
|
+
return $this->belongsToMany('App\Favorite', 'favorites');
|
38
|
+
}
|
39
|
+
public function user()
|
40
|
+
{
|
41
|
+
return $this->belongsTo('App\User');
|
42
|
+
}
|
43
|
+
```
|
44
|
+
**Category.php**
|
45
|
+
```
|
46
|
+
public function aidhias()
|
47
|
+
{
|
48
|
+
return $this->hasMany('App\Aidhia');
|
49
|
+
}
|
50
|
+
```
|
51
|
+
**Favorite.php**
|
52
|
+
```
|
53
|
+
class Favorite extends Model
|
54
|
+
{
|
55
|
+
//
|
56
|
+
}
|
57
|
+
```
|
58
|
+
**web.php**
|
59
|
+
```
|
60
|
+
Route::get('/my/favorite', 'ProductsController@myFavorite')->name('my.favorite');
|
61
|
+
```
|
62
|
+
**ProductsController.php**
|
63
|
+
```
|
64
|
+
public function myFavorite()
|
65
|
+
{
|
66
|
+
$user = Auth::user();
|
67
|
+
$favorites = $user->favorites;
|
9
68
|
|
69
|
+
return view('products.favoriteProducts', [
|
70
|
+
'favorites' => $favorites,
|
71
|
+
]);
|
72
|
+
}
|
73
|
+
```
|
10
|
-
|
74
|
+
**favoriteProducts.blade.php**
|
75
|
+
```
|
76
|
+
<div id="app">
|
77
|
+
<favorite-products :favorites="{{$favorites}}"></favorite-products>
|
78
|
+
</div>
|
79
|
+
```
|
80
|
+
**FavoriteProducts.vue**
|
81
|
+
```
|
82
|
+
<div v-for="product in favorites">
|
83
|
+
{{ product.category_id }} // ここにproductsテーブルのcategory_idとリレーションしたcategoriesテーブルのnameカラムの値を表示したい
|
84
|
+
</div>
|
85
|
+
|
86
|
+
<script>
|
87
|
+
export default {
|
88
|
+
props: {
|
89
|
+
favorites: Array,
|
90
|
+
},
|
91
|
+
mounted() {
|
92
|
+
console.log('Component mounted.')
|
93
|
+
}
|
94
|
+
}
|
95
|
+
</script>
|
96
|
+
```
|
1
訂正
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
https://teratail.com/questions/50857?link=qa_related_pc
|
2
2
|
この記事を参考にして、中間テーブルのリレーションを取得したのですがさらにここからリレーション先のリレーションを取得するにはどうしたらいいのでしょう。
|
3
|
-
例えば参考の記事で言うと
|
3
|
+
例えば参考の記事で言うとimagesテーブルにcategory_idというカラムがありcategoriesテーブルとリレーションしているとします。参考記事の方法でimagesテーブルの情報を取得し、さらにそこからそのimagesテーブルのcategory_idに紐付いたcategoriesテーブルの情報を取得するにはどのようにすればいいのでしょうか。
|
4
4
|
withを使い
|
5
5
|
|
6
6
|
```
|