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条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀