orderByを使用すると以下のようなエラーメッセージが出ます。
SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'order clause' (SQL: select * from `people` order by `` asc limit 5 offset 0)
以下は,orderByを使用しているコードです。
HelloController.php
Laravel
1 public function index(Request $request){ 2 $sort = $request->sort; 3 $items = Person::orderBy($sort, 'asc') 4 ->paginate(5); 5 $param = ['items' => $items, 'sort' => $sort]; 6 return view('hello.index', $param); 7 }
viewに関するコードはindex.blade.phpに表記しています。
Laravel
1@extends('layouts.helloapp') 2 3<style> 4 .pagination { font-size:10pt; } 5 .pagination li { display:inline-block } 6 tr th a:link { color: white; } 7 tr th a:visited { color: white; } 8 tr th a:hover { color: white; } 9 tr th a:active { color: white; } 10</style> 11 12@section('title', 'Index') 13 14@section('menubar') 15 @parent 16 インデックスぺージ 17@endsection 18 19@section('content') 20 <table> 21 <tr> 22 <th><a href="/hello?sort=name">name</a></th> 23 <th><a href="/hello?sort=mail">mail</a></th> 24 <th><a href="/hello?sort=age">age</a></th> 25 </tr> 26 @foreach ($items as $item) 27 <tr> 28 <td>{{$item->name}}</td> 29 <td>{{$item->mail}}</td> 30 <td>{{$item->age}}</td> 31 </tr> 32 @endforeach 33 </table> 34 {{ $items->appends(['sort' => $sort])->links() }} 35@endsection
先ほど,下記のコードに問題があると思い調べたところ,$sortが空ということが分かりました。
ですが,現状そこからどうしたらよいかわかりません。ご助言いただけると幸いです。
Laravel
1 $sort = $request->sort; 2 $items = Person::orderBy($sort, 'asc') 3 ->paginate(5);
以上です。何卒よろしくお願い申し上げます。。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/25 10:13
2018/11/25 11:43 編集
2018/11/26 13:39