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 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应