$pageName = 'p'; $perPage = 15; $currentPath = $request->url(); $query = $request->all(); $currentPage = $request->input($pageName,1); $offset = $perPage * ($currentPage - 1); $totalPages = DB::select('select count(*) as aggregate from posts')[0]->aggregate; $posts = DB::select('select * from posts limit :limit offset :offset',['limit'=>$perPage ,'offset'=>$offset]); $paginator = new LengthAwarePaginator($posts,$totalPages,$perPage,$currentPage,[ 'path' => $currentPath, 'query' => $query, //检索分页的情况 'pageName' => $pageName, ]); return view('post', ['posts' => $paginator]);
blade里
<table class="table table-bordered"> <tbody> @foreach ($posts as $post) <tr> <td>{{ $post->id }}</td> <td>{{ $post->name }}</td> </tr> @endforeach </tbody> </table> {{ $posts ->links() }}
顺便添加一下,从request里取得的url值
写道
"method":"GET"
"root":"http://localhost:8000"
"url":"http://localhost:8000/abc/def"
"fullUrl":"http://localhost:8000/abc/def?q1=123&q2=234"
"path":"abc/def"
"fullUrlWithQuery":"http://localhost:8000/abc/def?q1=123&q2=234&q=234"
"getHttpHost":"localhost:8000"
"getRequestUri":"/abc/def?q1=123&q2=234"
"getUri":"http://localhost:8000/abc/def?q1=123&q2=234"
"getQueryString":"q1=123&q2=234"
"getHost":"localhost"
"root":"http://localhost:8000"
"url":"http://localhost:8000/abc/def"
"fullUrl":"http://localhost:8000/abc/def?q1=123&q2=234"
"path":"abc/def"
"fullUrlWithQuery":"http://localhost:8000/abc/def?q1=123&q2=234&q=234"
"getHttpHost":"localhost:8000"
"getRequestUri":"/abc/def?q1=123&q2=234"
"getUri":"http://localhost:8000/abc/def?q1=123&q2=234"
"getQueryString":"q1=123&q2=234"
"getHost":"localhost"