##課題
formタグでpostしてDBに格納したデータを、created_atにしたがってlatest()で降順に並び替えて表示をしたいと考えています。
しかし一覧表示は相変わらずcreated_atにしたがった昇順になっており、どうやらlatest()が効いていないようです。
orderBy('created_at','desc')なども試しましたが変わりませんでした。
##実現したいこと
DB内のカラムをlatest()で降順に並び替えて表示したい
##対象のコード
TownController.php(一部抜粋)
php
1 public function tasTown() { 2 $list = House::all(); 3 return view('town.tasTown', compact('list')); 4 } 5 6 // DI(注入) 7 public function postHouse(FormSendRequest $request) 8 { 9 // POSTで受信したMapデータの登録 10 $map = new House(); 11 $map->name = $request->name; 12 $map->lat = $request->lat; 13 $map->lng = $request->lng; 14 $map->description = $request->description; 15 $map->suburb = $request->suburb; 16 $map->save(); 17 // Mapデータ取得(最新画面を表示->indexメソッドを呼びさせる redilect) 18 $list = House::->latest()->all(); 19 return redirect()->back(); 20 }
以下は表示させるためのbladeテンプレートです。
nswTown.blade.php(一部抜粋)
php
1 <!-- 一覧表示 --> 2 <table class="table table-hover table-bordered text-center"> 3 <thead class="thead-dark"> 4 <tr> 5 <th>名称</th> 6 <th>地域</th> 7 <th>登録日</th> 8 <th>削除</th> 9 </tr> 10 </thead> 11 <tbody> 12 @foreach($list as $val) 13 <tr class="table-info"> 14 <td><a href="/house/{{$val->id}}">{{$val->name}}</a></td> 15 <td>{{$val->suburb}}</td> 16 <td>{{$val->created_at}}</td> 17 <th><a href="#" data-id="{{ $val->id }}" class="del">削除</a></th> 18 <form method="post" action="{{ url('/houses', $val->id ) }}" id="form_{{ $val->id }}"> 19 {{ csrf_field() }} 20 {{ method_field('delete') }} 21 </form> 22 </tr> 23 @endforeach 24 </tbody> 25 </table>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/06 00:30
2019/05/08 17:32