質問編集履歴
5
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
Laravelでユーザーのお気に入りテーブルから商品情報とそのリレーションを貼った情報を表示する方法がわかりません。
|
2
2
|
|
3
|
-
追記:ここから
|
4
|
-
質問をシンプルにします
|
5
3
|
以下のコードで取得しています
|
6
4
|
```ここに言語を入力
|
7
5
|
$products = $user->favorites()->with('product')->get();
|
@@ -21,9 +19,10 @@
|
|
21
19
|
```
|
22
20
|
としていますが、うまく商品を取得出来ていません
|
23
21
|
2つに分けて取得するにはどうすればよいのでしょうか?
|
24
|
-
追記:ここまで
|
25
22
|
|
26
23
|
|
24
|
+
===ここから下は質問をシンプルにする前の質問ですので読み飛ばしてください===
|
25
|
+
|
27
26
|
やってみたことは
|
28
27
|
$products = $user->favorites()->with('product')->get();
|
29
28
|
|
4
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -62,66 +62,4 @@
|
|
62
62
|
|
63
63
|
コレクションであったり、何を返しているのかがよくわかっていない面もありますので
|
64
64
|
どうしたらよいのでしょうか?
|
65
|
-
よろしくお願い致します
|
65
|
+
よろしくお願い致します
|
66
|
-
|
67
|
-
追記:
|
68
|
-
説明がうまくできず申し訳ございません。
|
69
|
-
やりたいことは
|
70
|
-
まず、お気に入りテーブルから登録してる商品のIDを取得します
|
71
|
-
レコードが5件あった場合、
|
72
|
-
その5件の商品情報を取得するためのコードが知りたいです
|
73
|
-
|
74
|
-
お気に入り
|
75
|
-
```ここに言語を入力
|
76
|
-
class Favorite extends Model
|
77
|
-
{
|
78
|
-
//
|
79
|
-
protected $fillable = [
|
80
|
-
'user_id',
|
81
|
-
'product_id',
|
82
|
-
];
|
83
|
-
|
84
|
-
public function user()
|
85
|
-
{
|
86
|
-
return $this->belongsTo('App\User');
|
87
|
-
}
|
88
|
-
|
89
|
-
public function product()
|
90
|
-
{
|
91
|
-
return $this->belongsTo('App\Product');
|
92
|
-
}
|
93
|
-
}
|
94
|
-
```
|
95
|
-
|
96
|
-
商品
|
97
|
-
|
98
|
-
```ここに言語を入力
|
99
|
-
class Product extends Model
|
100
|
-
{
|
101
|
-
//
|
102
|
-
use SoftDeletes;
|
103
|
-
|
104
|
-
// protected $table = 'Product';
|
105
|
-
protected $dates = [
|
106
|
-
'deleted_at',
|
107
|
-
'start_date',
|
108
|
-
'end_date',
|
109
|
-
'haisou_start',
|
110
|
-
];
|
111
|
-
|
112
|
-
public function productComments()
|
113
|
-
{
|
114
|
-
return $this->hasMany('App\ProductComment');
|
115
|
-
}
|
116
|
-
|
117
|
-
public function favorites()
|
118
|
-
{
|
119
|
-
return $this->hasMany('App\Favorite');
|
120
|
-
}
|
121
|
-
|
122
|
-
public function productImages()
|
123
|
-
{
|
124
|
-
return $this->hasMany('App\ProductImage');
|
125
|
-
}
|
126
|
-
}
|
127
|
-
```
|
3
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,6 +1,33 @@
|
|
1
1
|
Laravelでユーザーのお気に入りテーブルから商品情報とそのリレーションを貼った情報を表示する方法がわかりません。
|
2
2
|
|
3
|
+
追記:ここから
|
4
|
+
質問をシンプルにします
|
5
|
+
以下のコードで取得しています
|
3
6
|
```ここに言語を入力
|
7
|
+
$products = $user->favorites()->with('product')->get();
|
8
|
+
```
|
9
|
+
これをお気に入りの取得と
|
10
|
+
取得したお気に入りの結果から商品情報を取得
|
11
|
+
の2つに分けたいです。
|
12
|
+
|
13
|
+
やってみたこととして
|
14
|
+
```ここに言語を入力
|
15
|
+
お気に入りの取得
|
16
|
+
$favorites = Favorite::where('user_id', $user->id)->get();
|
17
|
+
結果から商品を取得
|
18
|
+
$products = Product::where('product_id', $favorites)->get();
|
19
|
+
や
|
20
|
+
$products = Product::where('product_id', $favorites->product_id)->get();
|
21
|
+
```
|
22
|
+
としていますが、うまく商品を取得出来ていません
|
23
|
+
2つに分けて取得するにはどうすればよいのでしょうか?
|
24
|
+
追記:ここまで
|
25
|
+
|
26
|
+
|
27
|
+
やってみたことは
|
28
|
+
$products = $user->favorites()->with('product')->get();
|
29
|
+
|
30
|
+
```ここに言語を入力
|
4
31
|
//お気に入りとその商品情報を取得
|
5
32
|
$products = $user->favorites()->with('product')->get();
|
6
33
|
```
|
2
モデルの追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -42,4 +42,59 @@
|
|
42
42
|
やりたいことは
|
43
43
|
まず、お気に入りテーブルから登録してる商品のIDを取得します
|
44
44
|
レコードが5件あった場合、
|
45
|
-
その5件の商品情報を取得するためのコードが知りたいです
|
45
|
+
その5件の商品情報を取得するためのコードが知りたいです
|
46
|
+
|
47
|
+
お気に入り
|
48
|
+
```ここに言語を入力
|
49
|
+
class Favorite extends Model
|
50
|
+
{
|
51
|
+
//
|
52
|
+
protected $fillable = [
|
53
|
+
'user_id',
|
54
|
+
'product_id',
|
55
|
+
];
|
56
|
+
|
57
|
+
public function user()
|
58
|
+
{
|
59
|
+
return $this->belongsTo('App\User');
|
60
|
+
}
|
61
|
+
|
62
|
+
public function product()
|
63
|
+
{
|
64
|
+
return $this->belongsTo('App\Product');
|
65
|
+
}
|
66
|
+
}
|
67
|
+
```
|
68
|
+
|
69
|
+
商品
|
70
|
+
|
71
|
+
```ここに言語を入力
|
72
|
+
class Product extends Model
|
73
|
+
{
|
74
|
+
//
|
75
|
+
use SoftDeletes;
|
76
|
+
|
77
|
+
// protected $table = 'Product';
|
78
|
+
protected $dates = [
|
79
|
+
'deleted_at',
|
80
|
+
'start_date',
|
81
|
+
'end_date',
|
82
|
+
'haisou_start',
|
83
|
+
];
|
84
|
+
|
85
|
+
public function productComments()
|
86
|
+
{
|
87
|
+
return $this->hasMany('App\ProductComment');
|
88
|
+
}
|
89
|
+
|
90
|
+
public function favorites()
|
91
|
+
{
|
92
|
+
return $this->hasMany('App\Favorite');
|
93
|
+
}
|
94
|
+
|
95
|
+
public function productImages()
|
96
|
+
{
|
97
|
+
return $this->hasMany('App\ProductImage');
|
98
|
+
}
|
99
|
+
}
|
100
|
+
```
|
1
説明追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -35,4 +35,11 @@
|
|
35
35
|
|
36
36
|
コレクションであったり、何を返しているのかがよくわかっていない面もありますので
|
37
37
|
どうしたらよいのでしょうか?
|
38
|
-
よろしくお願い致します
|
38
|
+
よろしくお願い致します
|
39
|
+
|
40
|
+
追記:
|
41
|
+
説明がうまくできず申し訳ございません。
|
42
|
+
やりたいことは
|
43
|
+
まず、お気に入りテーブルから登録してる商品のIDを取得します
|
44
|
+
レコードが5件あった場合、
|
45
|
+
その5件の商品情報を取得するためのコードが知りたいです
|