teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

4

文の追加

2020/06/25 13:38

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -12,7 +12,6 @@
12
12
  image(写真)は載せないことにしたので無視してください。
13
13
  よろしくお願いします。
14
14
 
15
-
16
15
  下記のコードはマイページの注文一覧をしようとしたコードです
17
16
  アドバイスをもとにいろいろやってよくわからない形になり、いきず待ってしまったのでアドバイスいただけると幸いです。
18
17
  現状お店に対して、ほかの注文した商品も入っているところです。

3

文の追加

2020/06/25 13:38

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -14,6 +14,7 @@
14
14
 
15
15
 
16
16
  下記のコードはマイページの注文一覧をしようとしたコードです
17
+ アドバイスをもとにいろいろやってよくわからない形になり、いきず待ってしまったのでアドバイスいただけると幸いです。
17
18
  現状お店に対して、ほかの注文した商品も入っているところです。
18
19
  UserController
19
20
  ```ここに言語を入力
@@ -41,18 +42,17 @@
41
42
  {
42
43
  public function show($id)
43
44
  {
44
- $reservations = Reservation::where('user_id', $id)->latest()->get();
45
+ $reservations = DB::table('reservations')->where('user_id', $id)->latest()->get();
45
- $shops_id = $reservations->pluck('shop_id');
46
- $cc = $reservations->pluck('created_at');
47
- $shop_id = $cc->unique();
48
46
  $res_shops = array();
47
+ foreach ($reservations->groupBy('shop_id.created_at') as $re) {
48
+ $shop_id = $re->pluck('shop_id');
49
- foreach ($shop_id as $sh_id) {
49
+ foreach ($shop_id as $sh_id) {
50
- $shop = Shop::where('id', $sh_id)->get();
50
+ $shop = Shop::where('id', $sh_id)->get();
51
-
51
+ }
52
- array_push($res_shops,$shop);
52
+ array_push($res_shops, $shop);
53
53
  }
54
54
  $commodities = array();
55
- $commodity_id = $reservations->pluck('commodity_id');
55
+ $commodity_id = $re->pluck('commodity_id');
56
56
  foreach ($commodity_id as $com_id) {
57
57
  $commodity = Commodity::where('id', $com_id)->get();
58
58
  array_push($commodities,$commodity);

2

文の追加

2020/06/25 09:08

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -9,6 +9,7 @@
9
9
 
10
10
  返信遅くなってしまいすみません。
11
11
  migrate関連DB関連載せておきました。
12
+ image(写真)は載せないことにしたので無視してください。
12
13
  よろしくお願いします。
13
14
 
14
15
 

1

文の追加

2020/06/20 09:25

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -7,9 +7,11 @@
7
7
  user_idとshop_idとcreated_atで複数注文したものをまとめて取ってくるのかなと思っています。
8
8
  同じお店の注文でも注文した日時が違ったら、表示を別にしたいです。
9
9
 
10
+ 返信遅くなってしまいすみません。
11
+ migrate関連DB関連載せておきました。
12
+ よろしくお願いします。
10
13
 
11
14
 
12
-
13
15
  下記のコードはマイページの注文一覧をしようとしたコードです
14
16
  現状お店に対して、ほかの注文した商品も入っているところです。
15
17
  UserController
@@ -57,4 +59,175 @@
57
59
  return view("user.show", 'commodities' => $commodities, 'reservations' => $reservations, 'res_shops' => $res_shops, 'fav_shops' => $fav_shops]);
58
60
  }
59
61
  }
60
- ```
62
+ ```
63
+ **User.php**
64
+ ```
65
+ <?php
66
+
67
+ namespace App;
68
+
69
+ use Illuminate\Contracts\Auth\MustVerifyEmail;
70
+ use Illuminate\Foundation\Auth\User as Authenticatable;
71
+ use Illuminate\Notifications\Notifiable;
72
+
73
+ class User extends Authenticatable
74
+ {
75
+ use Notifiable;
76
+
77
+ /**
78
+ * The attributes that are mass assignable.
79
+ *
80
+ * @var array
81
+ */
82
+ protected $fillable = [
83
+ 'name', 'email', 'password',
84
+ ];
85
+
86
+ /**
87
+ * The attributes that should be hidden for arrays.
88
+ *
89
+ * @var array
90
+ */
91
+ protected $hidden = [
92
+ 'password', 'remember_token',
93
+ ];
94
+
95
+ /**
96
+ * The attributes that should be cast to native types.
97
+ *
98
+ * @var array
99
+ */
100
+ protected $casts = [
101
+ 'email_verified_at' => 'datetime',
102
+ ];
103
+
104
+ public function shops()
105
+ {
106
+ return $this->hasMany('App\Shop', 'favorites', 'user_id', 'shop_id')->withTimestamps();
107
+ }
108
+
109
+
110
+ public function reservation()
111
+ {
112
+ return $this->belongsTo('App\Reservation');
113
+ }
114
+
115
+ }
116
+ ```
117
+ **Shop.php**
118
+ ```ここに言語を入力
119
+ <?php
120
+
121
+ namespace App;
122
+
123
+ use Illuminate\Database\Eloquent\Model;
124
+
125
+ class Shop extends Model
126
+ {
127
+ protected $fillable = [
128
+ 'sname',
129
+ 'region',
130
+ 'sprice',
131
+ 'detail',
132
+ 'store_in',
133
+ 'take_out',
134
+ 'delivery',
135
+ ];
136
+
137
+ public function commodities()
138
+ {
139
+ return $this->hasMany('App\Commodity');
140
+ }
141
+
142
+ public function images()
143
+ {
144
+ return $this->hasMany('App\Image');
145
+ }
146
+
147
+ public function users()
148
+ {
149
+ return $this->belongsToMany('App\User', 'favorites', 'shop_id', 'user_id');
150
+ }
151
+
152
+
153
+ public function reservation()
154
+ {
155
+ return $this->belongsTo('App\Reservation');
156
+ }
157
+ }
158
+ ```
159
+ **Commodity.php**
160
+ ```ここに言語を入力
161
+ <?php
162
+
163
+ namespace App;
164
+
165
+ use Illuminate\Database\Eloquent\Model;
166
+
167
+ class Commodity extends Model
168
+ {
169
+
170
+ protected $fillable = [
171
+ 'name',
172
+ 'price',
173
+ 'description',
174
+ ];
175
+
176
+ public function images()
177
+ {
178
+ return $this->hasMany('App\Image');
179
+ }
180
+
181
+ public function reservations()
182
+ {
183
+ return $this->hasMany('App\Reservation');
184
+ }
185
+
186
+ public function user()
187
+ {
188
+ return $this->belongsTo('App\User');
189
+ }
190
+
191
+ public function shop()
192
+ {
193
+ return $this->belongsTo('App\Shop');
194
+ }
195
+ }
196
+ ```
197
+ **Reservation.php**
198
+ ```ここに言語を入力
199
+ <?php
200
+
201
+ namespace App;
202
+
203
+ use Illuminate\Database\Eloquent\Model;
204
+
205
+ class Reservation extends Model
206
+ {
207
+ protected $fillable = [
208
+ 'remark',
209
+ ];
210
+
211
+ public function images()
212
+ {
213
+ return $this->hasMany('App\Image');
214
+ }
215
+
216
+ public function shops()
217
+ {
218
+ return $this->hasMany('App\Shop');
219
+ }
220
+
221
+ public function user()
222
+ {
223
+ return $this->belongsTo('App\User');
224
+ }
225
+
226
+ public function commodity()
227
+ {
228
+ return $this->belongdTo('App\Commodity');
229
+ }
230
+ }
231
+ ```
232
+ **DB Reservationテーブル**
233
+ ![](3ccafb6abf04a78ffe44359debc9f65e.jpeg)