drlu11748 2015-10-19 03:12
浏览 85
已采纳

Laravel 5 Pagination按用户输入排序

i want an option for the user to sort the pagination from either in ascending or descending order. i tried the code below but i dont know how to insert the selected value.

index.blade.php

   <select name="orderby" id="orderby">
                <option value="asc">Ascending</option>
                <option value="desc">Descending</option>
            </select>

    {!! $finding_tbl->appends(['sort' => 'here'])->render() !!}

here is my index in PagesController

 public function index()
    {
        $sort =$_GET['sort'];
        $finding_tbl = findings::orderBy('id', $sort)->paginate(5);
        $finding_tbl->setPath('home');
        return view('pages.index',compact('finding_tbl','sort') );
    }
  • 写回答

2条回答 默认 最新

  • doufu9836 2015-10-19 04:39
    关注

    The recommended method to access your get variable (if your using Laravel 5) is to obtain an instance of the request via dependency injection. To quote the documentation:

    To obtain an instance of the current HTTP request via dependency injection, you should type-hint the Illuminate\Http\Request class on your controller constructor or method.

    In your case, your controller would look something like this.

    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    use Illuminate\Routing\Controller;
    
    class PagesController extends Controller
    {
    
        public function index(Request $request)
        {
            $sort = $request->input('sort', 'asc');
            $finding_tbl = findings::orderBy('id', $sort)->paginate(5);
            $finding_tbl->setPath('home');
            return view('pages.index',compact('finding_tbl','sort') );
        }
    }
    

    Once you have an instance you can access any input variables (GET/POST) like so:

    $sort = $request->input('sort');
    

    If you need to define a default value for when the variable doesn't exist this can be passed as the second parameter.

    $sort = $request->input('sort', 'asc');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?