質問編集履歴

4

回答を受けての追記③

2018/05/23 08:44

投稿

kazoogon
kazoogon

スコア281

test CHANGED
File without changes
test CHANGED
@@ -207,3 +207,87 @@
207
207
   →そのjsonを利用してhtmlに記載すると表示される
208
208
 
209
209
  (**history.teacher.teacher_images[0].name** のように記載(vue.js))
210
+
211
+
212
+
213
+ 回答を受けての追記③
214
+
215
+ ---
216
+
217
+ ```
218
+
219
+ //こちらが授業可能な先生情報を取ってくるSQL文
220
+
221
+ $available_teachers = Reservation_calender::where('date', '=', $date)
222
+
223
+ ->where(function($query) use ($user_id){
224
+
225
+ $query->where('user_id', '=', NULL);
226
+
227
+ })->with(['teacher.teacherImages' => function($query){//relationのまたそのrelation
228
+
229
+ $query->where('profile', '=', 1)->latest('created_at');
230
+
231
+ }])->get();
232
+
233
+
234
+
235
+ //こんな感じでbladeファイルに値を渡します
236
+
237
+ return view('mypage.main')->with([
238
+
239
+ 'available_teachers' => $available_teachers,
240
+
241
+ ]);
242
+
243
+
244
+
245
+
246
+
247
+ //bladeファイルのscriptタグ内にてJSON化
248
+
249
+ var available_teachers = @json($available_teachers);
250
+
251
+
252
+
253
+ //html内にて画像の表示→表示される
254
+
255
+ <img class="thumb img-circle" :src="'/images/teachers/'+ available_teacher.teacher.teacher_images[0].name" alt="profile" width=110 height=110>
256
+
257
+ ```
258
+
259
+
260
+
261
+ ```
262
+
263
+ //こちらが今回質問させていただいた所のSQL文
264
+
265
+ $histories = Reservation_calender::where('user_id', '=' ,$user_id)
266
+
267
+ ->where('date', '<' ,$date)
268
+
269
+ ->with('time')
270
+
271
+ ->with(['teacher.teacherImages' => function($query){//relationのまたそのrelation
272
+
273
+ $query->where('profile', '=', 1)->latest('created_at');
274
+
275
+ }])->latest('date')->get();
276
+
277
+
278
+
279
+ //同じように値を渡す
280
+
281
+ return view('mypage.history')->with([
282
+
283
+ 'histories' => $histories,
284
+
285
+ ]);
286
+
287
+
288
+
289
+ //画像表示(teacherImagesとする必要有)
290
+
291
+ <img class="thumb img-circle" src="/images/teachers/{{$history->teacher->teacherImages[0]->name}}" alt="profile" width=70 height=70>
292
+
293
+ ```

3

回答を受けての追記②

2018/05/23 08:44

投稿

kazoogon
kazoogon

スコア281

test CHANGED
File without changes
test CHANGED
@@ -155,3 +155,55 @@
155
155
  @endforeach
156
156
 
157
157
  ```
158
+
159
+
160
+
161
+ 回答を受けての追記②
162
+
163
+ ---
164
+
165
+ ```
166
+
167
+ //サーバ側でsql取得できるように設定
168
+
169
+ $histories = Reservation_calender::where('user_id', '=' ,$user_id)
170
+
171
+ ->where('date', '<' ,$date)
172
+
173
+ ->with('time')
174
+
175
+ ->with(['teacher.teacherImages' => function($query){
176
+
177
+ $query->where('profile', '=', 1)->latest('created_at');
178
+
179
+ }])->latest('date')->toSql();//元々は ->get();
180
+
181
+
182
+
183
+ var_dump($histories);
184
+
185
+ ```
186
+
187
+ ```
188
+
189
+ //実行結果
190
+
191
+ 'select * from `reservation_calenders` where `user_id` = ? and `date` < ? order by `date` desc'
192
+
193
+ ```
194
+
195
+
196
+
197
+ とろうとしていないようですね。。しかしなぜlogには出力されるのか謎ですが。。
198
+
199
+
200
+
201
+ ちなみに参考になるか分かりませんが、
202
+
203
+ 他のページでも似たように先生の情報を取る
204
+
205
+  →ajax使用するためjs側にてjson形式にする
206
+
207
+  →そのjsonを利用してhtmlに記載すると表示される
208
+
209
+ (**history.teacher.teacher_images[0].name** のように記載(vue.js))

2

回答を受けての追記

2018/05/18 08:17

投稿

kazoogon
kazoogon

スコア281

test CHANGED
File without changes
test CHANGED
@@ -115,3 +115,43 @@
115
115
 
116
116
 
117
117
  (またmodelでのrelation設定など、追記してほしいコードがあればおっしゃってください。)
118
+
119
+
120
+
121
+ 回答を受けての追記
122
+
123
+ ---
124
+
125
+ ```
126
+
127
+ @foreach($histories as $history)
128
+
129
+ <li>
130
+
131
+ <div class="detail">
132
+
133
+ <div class="detail-img">
134
+
135
+ <img class="thumb img-circle" src="/images/teachers/{{$history->teacher->teacher_images[0]->name}}" alt="profile" width=70 height=70>//最終的にはファイル名をこのようにlaravelからとってきたい
136
+
137
+ </div>
138
+
139
+ <div class="detail-txt">
140
+
141
+ <div class="date">{{$history->date}}</div>
142
+
143
+ <div class="time">{{$history->time->time}}</div>
144
+
145
+ <div class="name"><a href=""></a>{{$history->teacher->name}}</div>
146
+
147
+ {{$history->teacher->teacher_images}}//この部分browser上では何も表示されない
148
+
149
+ </div>
150
+
151
+ </div>
152
+
153
+ </li>
154
+
155
+ @endforeach
156
+
157
+ ```

1

修正

2018/05/18 05:34

投稿

kazoogon
kazoogon

スコア281

test CHANGED
File without changes
test CHANGED
@@ -98,9 +98,9 @@
98
98
 
99
99
 
100
100
 
101
- コメントで書き込んだ通りですが、teacher_imagesのnameを使用させたい、しかし```{{$histories->teacher->teacehr_images[0]->name}}```とするが```Trying to get property 'name' of non-object```のエラー
101
+ コメントで書き込んだ通りですが、teacher_imagesのnameを使用させたい、しかし```{{$histories->teacher->teacher_images[0]->name}}```とするが```Trying to get property 'name' of non-object```のエラー
102
102
 
103
- →そこで```{{$histories->teacher->teacehr_images}}```とし、確認すると何もhtml上に表示されない。
103
+ →そこで```{{$histories->teacher->teacher_images}}```とし、確認すると何もhtml上に表示されない。
104
104
 
105
105
 
106
106