How to use pagination with laravel DB::select query


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

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


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.title,notices.body,notices.created_at,notices.updated_at,,departments.department_name
    FROM notices
    INNER JOIN users ON notices.user_id =
    INNER JOIN departments on users.dpt_id =
    ORDER BY DESC')->paginate(20);

    $result = new Paginator($notices,2,1,[]);

    return view('welcome')->with('allNotices', $notices);



$notices = DB::table('notices')
        ->join('users', 'notices.user_id', '=', '')
        ->join('departments', 'users.dpt_id', '=', '')
        ->select('', 'notices.title', 'notices.body', 'notices.created_at', 'notices.updated_at', '', 'departments.department_name')

Answered By – MohamedSabil83

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published