Uma*_*Gul 7 php mysql pagination laravel
我正在使用Laravel中的一个项目并使用DB facade来运行sql的原始查询.在我的情况下,我使用DB :: select,问题是分页方法不能使用此DB原始查询并显示此错误
Call to a member function paginate() on array
Run Code Online (Sandbox Code Playgroud)
我只想要如何用DB原始查询实现laravel分页这里是我的代码:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Notice;
use Illuminate\Support\Facades\DB;
use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
class NoticeController extends Controller
{
public function index(){
$notices = DB::select('select
notices.id,notices.title,notices.body,notices.created_at,notices.updated_at,
users.name,departments.department_name
FROM notices
INNER JOIN users ON notices.user_id = users.id
INNER JOIN departments on users.dpt_id = departments.id
ORDER BY users.id DESC')->paginate(20);
$result = new Paginator($notices,2,1,[]);
return view('welcome')->with('allNotices', $notices);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 15
public function index(Request $request){
$notices = DB::select('select notices.id,notices.title,notices.body,notices.created_at,notices.updated_at,
users.name,departments.department_name
FROM notices
INNER JOIN users ON notices.user_id = users.id
INNER JOIN departments on users.dpt_id = departments.id
ORDER BY users.id DESC');
$notices = $this->arrayPaginator($notices, $request);
return view('welcome')->with('allNotices', $notices);
}
public function arrayPaginator($array, $request)
{
$page = Input::get('page', 1);
$perPage = 10;
$offset = ($page * $perPage) - $perPage;
return new LengthAwarePaginator(array_slice($array, $offset, $perPage, true), count($array), $perPage, $page,
['path' => $request->url(), 'query' => $request->query()]);
}
Run Code Online (Sandbox Code Playgroud)
尝试:
$notices = DB::table('notices')
->join('users', 'notices.user_id', '=', 'users.id')
->join('departments', 'users.dpt_id', '=', 'departments.id')
->select('notices.id', 'notices.title', 'notices.body', 'notices.created_at', 'notices.updated_at', 'users.name', 'departments.department_name')
->paginate(20);
Run Code Online (Sandbox Code Playgroud)
小智 5
永远不要在 php 端使用分页逻辑!对您的 sql 使用限制和偏移量,并将其余部分留给数据库服务器。另外为您的语句使用单独的计数选择。
数数:
$sql_count = 'SELECT count(1) cnt FROM ('. $sql . ') x';
$result = \DB::select( DB::raw($sql_count) );
$data['count'] = $result[0]->cnt;
Run Code Online (Sandbox Code Playgroud)
结果:
$sql .= ' LIMIT ' . $offset . ', ' . $limit;
$result = \DB::select( DB::raw($sql) );
$myPaginator = new \Illuminate\Pagination\LengthAwarePaginator($result, $data['count'], $limit, $page, ['path' => action('MyController@index')]);
$data['result'] = $result;
Run Code Online (Sandbox Code Playgroud)
小智 5
这个适合我
$sql = "some sql code";
$page = 1;
$size = 10;
$data = DB::select($sql);
$collect = collect($data);
$paginationData = new LengthAwarePaginator(
$collect->forPage($page, $size),
$collect->count(),
$size,
$page
);
Run Code Online (Sandbox Code Playgroud)
小智 5
它对我有用,请参阅\n在控制器中首次使用
\nuse Illuminate\\Pagination\\Paginator;\nRun Code Online (Sandbox Code Playgroud)\n然后在函数中
\n$query = DB::select(DB::raw("SELECT pro.* , (SELECT TIMESTAMPDIFF(DAY,updated_at,'$current_date') from users as u where u.id=pro.id) as days FROM users as pro where role_id = 6 and delete_status=0 and user_status = 'A' and approved_status = 1 and is_clever_courier = 1 having days >= 5"));\n\n \n$page1 = new Paginator($query, $maxPage);\n\ndd($page1);\nRun Code Online (Sandbox Code Playgroud)\n欧/普 =>
\nPaginator {#1450 \xe2\x96\xbc\n #hasMore: true\n #items: Collection {#1509 \xe2\x96\xbc\n #items: array:10 [\xe2\x96\xbc\n 0 => {#1454 \xe2\x96\xb6}\n 1 => {#1455 \xe2\x96\xb6}\n 2 => {#1456 \xe2\x96\xb6}\n 3 => {#1457 \xe2\x96\xb6}\n 4 => {#1458 \xe2\x96\xb6}\n 5 => {#1459 \xe2\x96\xb6}\n 6 => {#1460 \xe2\x96\xb6}\n 7 => {#1461 \xe2\x96\xb6}\n 8 => {#1462 \xe2\x96\xb6}\n 9 => {#1463 \xe2\x96\xb6}\n ]\n }\n #perPage: 10\n #currentPage: 1\n #path: "/"\n #query: []\n #fragment: null\n #pageName: "page"\n +onEachSide: 3\n #options: []\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
19443 次 |
| 最近记录: |