laravel5.5にて学校システム構築中
実現したいこと
「ネストしたテーブルの情報をブラウザー上に表示させる」
//phpにて自分の授業情報をとってくる $histories = Reservation_calender::where('user_id', '=' ,$user_id) ->where('date', '<' ,$date) ->with('time') ->with(['teacher.teacherImages' => function($query){//relationのまたそのrelation $query->where('profile', '=', 1)->latest('created_at'); }])->latest('date')->get();
//var_dump($histories)にて出力させた結果がこちら { "id":749 "time_id":7 "teacher_id":1 "user_id":1 "date":"2018-04-30" "created_at":"2018-04-28 01:17:33" "updated_at":"2018-04-28 01:17:33" "time":{ "id":7 "time":"16:00-17:00" "created_at":null "updated_at":null } "teacher":{ "id":1 "name":"goku sensei" "email":"sample@gmail.com" "created_at":"2018-03-18 14:55:36" "updated_at":"2018-03-18 14:55:36" "comment":"How are you???" "teacher_images":[{ "id":5 "teacher_id":1 "profile":1 "name":"5adef89b565a6"//これが画像ファイルの名前、これをimgタグに挿入し画像を表示したい "created_at":"2018-04-24 09:27:55" "updated_at":"2018-04-24 09:27:55" }] } }
コメントで書き込んだ通りですが、teacher_imagesのnameを使用させたい、しかし{{$histories->teacher->teacher_images[0]->name}}
とするがTrying to get property 'name' of non-object
のエラー
→そこで{{$histories->teacher->teacher_images}}
とし、確認すると何もhtml上に表示されない。
また{{$histories->time->time}}
は16:00-17:00と表示されます
なぜネストしたteacher_imagesの情報はとってこれているのに、htmlに表示させようとすると何も表示されないのか不明です。
よろしくお願いします。
(またmodelでのrelation設定など、追記してほしいコードがあればおっしゃってください。)
回答を受けての追記
@foreach($histories as $history) <li> <div class="detail"> <div class="detail-img"> <img class="thumb img-circle" src="/images/teachers/{{$history->teacher->teacher_images[0]->name}}" alt="profile" width=70 height=70>//最終的にはファイル名をこのようにlaravelからとってきたい </div> <div class="detail-txt"> <div class="date">{{$history->date}}</div> <div class="time">{{$history->time->time}}</div> <div class="name"><a href=""></a>{{$history->teacher->name}}</div> {{$history->teacher->teacher_images}}//この部分browser上では何も表示されない </div> </div> </li> @endforeach
回答を受けての追記②
//サーバ側でsql取得できるように設定 $histories = Reservation_calender::where('user_id', '=' ,$user_id) ->where('date', '<' ,$date) ->with('time') ->with(['teacher.teacherImages' => function($query){ $query->where('profile', '=', 1)->latest('created_at'); }])->latest('date')->toSql();//元々は ->get(); var_dump($histories);
//実行結果 'select * from `reservation_calenders` where `user_id` = ? and `date` < ? order by `date` desc'
とろうとしていないようですね。。しかしなぜlogには出力されるのか謎ですが。。
ちなみに参考になるか分かりませんが、
他のページでも似たように先生の情報を取る
→ajax使用するためjs側にてjson形式にする
→そのjsonを利用してhtmlに記載すると表示される
(history.teacher.teacher_images[0].name のように記載(vue.js))
回答を受けての追記③
//こちらが授業可能な先生情報を取ってくるSQL文 $available_teachers = Reservation_calender::where('date', '=', $date) ->where(function($query) use ($user_id){ $query->where('user_id', '=', NULL); })->with(['teacher.teacherImages' => function($query){//relationのまたそのrelation $query->where('profile', '=', 1)->latest('created_at'); }])->get(); //こんな感じでbladeファイルに値を渡します return view('mypage.main')->with([ 'available_teachers' => $available_teachers, ]); //bladeファイルのscriptタグ内にてJSON化 var available_teachers = @json($available_teachers); //html内にて画像の表示→表示される <img class="thumb img-circle" :src="'/images/teachers/'+ available_teacher.teacher.teacher_images[0].name" alt="profile" width=110 height=110>
//こちらが今回質問させていただいた所のSQL文 $histories = Reservation_calender::where('user_id', '=' ,$user_id) ->where('date', '<' ,$date) ->with('time') ->with(['teacher.teacherImages' => function($query){//relationのまたそのrelation $query->where('profile', '=', 1)->latest('created_at'); }])->latest('date')->get(); //同じように値を渡す return view('mypage.history')->with([ 'histories' => $histories, ]); //画像表示(teacherImagesとする必要有) <img class="thumb img-circle" src="/images/teachers/{{$history->teacher->teacherImages[0]->name}}" alt="profile" width=70 height=70>
回答1件
あなたの回答
tips
プレビュー