如何在Laravel DB :: select查询中使用分页

编程入门 行业动态 更新时间:2024-10-27 16:31:44
本文介绍了如何在Laravel DB :: select查询中使用分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在Laravel中进行一个项目,并使用DB Facade来运行sql的原始查询. 就我而言,我使用的是DB :: select,问题是分页方法不适用于该数据库原始查询并显示此错误

I am working on a project in Laravel and using DB facade to run raw queries of sql. In my case I am using DB::select, problem is that pagination method is not working with this DB raw query and showing this error

Call to a member function paginate() on array

我只想如何使用DB原始查询实现laravel分页 这是我的代码:

I just want how to implement laravel pagination with DB raw queries here is my code:

<?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); } }

推荐答案

尝试:

$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);

更多推荐

如何在Laravel DB :: select查询中使用分页

本文发布于:2023-10-21 17:06:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1514825.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分页   如何在   DB   Laravel   select

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!