Laravel 5 Pagination with Query Builder

laravel pagination
laravel paginate collection
laravel pagination with search
laravel manual pagination
laravel query builder
laravel pagination offset
laravel pagination parameters
laravel paginate array

I'm making a Laravel Pagination based from my query result and be rendered in my view. I'm following this guide http://laravel.com/docs/5.1/pagination but I get an error:

Call to a member function paginate() on a non-object

I'm using query builder so I think that should be ok? Here's my code

public function getDeliveries($date_from, $date_to)
{
    $query = "Select order_confirmation.oc_number as oc,
    order_confirmation.count as cnt,
    order_confirmation.status as stat,
    order_confirmation.po_number as pon,
    order_summary.date_delivered as dd,
    order_summary.delivery_quantity as dq,
    order_summary.is_invoiced as iin,
    order_summary.filename as fn,
    order_summary.invoice_number as inum,
    order_summary.oc_idfk as ocidfk,
    order_summary.date_invoiced as di
    FROM
    order_confirmation,order_summary
    where order_confirmation.id = order_summary.oc_idfk";

    if (isset($date_from)) {
        if (!empty($date_from))
        {
            $query .= " and order_summary.date_delivered >= '".$date_from."'";
        }
    }

    if (isset($date_to)) {
        if (!empty($date_to)) 
        {
            $query .= " and order_summary.date_delivered <= '".$date_to."'";
        }
    }

    $query.="order by order_confirmation.id ASC";

    $data = DB::connection('qds106')->select($query)->paginate(15);
    return $data;
}

However when I remove the paginate(15); it works fine.

Thanks


in the doc at this page: http://laravel.com/docs/5.1/pagination we can see that we are not forced to use eloquent.

$users = DB::table('users')->paginate(15);

but, be sure you don't make a groupBy in your query because, the paginate method uses it.

after i'm no sure you can use paginate with query builder ( select($query) )

--- edit

You can create collection an use the paginator class :

$collection = new Collection($put_your_array_here);

// Paginate
$perPage = 10; // Item per page
$currentPage = Input::get('page') - 1; // url.com/test?page=2
$pagedData = $collection->slice($currentPage * $perPage, $perPage)->all();
$collection= Paginator::make($pagedData, count($collection), $perPage);

and in your view just use $collection->render();

Pagination - Laravel, in the doc at this page: http://laravel.com/docs/5.1/pagination we can see that we are not forced to use eloquent. $users = DB::table('users')->paginate(15); but, be sure you don't make a groupBy in your query because, the paginate method uses it. You set it by using the custom pagination.. In other frameworks, pagination can be very painful. Laravel's paginator is integrated with the query builder and Eloquent ORM and provides convenient, easy-to-use pagination of database results out of the box. The HTML generated by the paginator is compatible with the Bootstrap CSS framework. Basic Usage. Paginating Query Builder Results


public function getDeliveries($date_from, $date_to)
{
    $query="your_query_here";
    $deliveries = DB::select($query);

    $deliveries = collect($deliveries);
    $perPage = 10;
    $currentPage = \Input::get('page') ?: 1;
    $slice_init = ($currentPage == 1) ? 0 : (($currentPage*$perPage)-$perPage);
    $pagedData = $users->slice($slice_init, $perPage)->all();
    $deliveries = new LengthAwarePaginator($pagedData, count($deliveries), $perPage, $currentPage);
    $deliveries ->setPath('set_your_link_page');
    return $deliveries;
 }

Laravel 5 Pagination with Query Builder, # Laravel Custom Pagination. Manually Custom Query the database: Using Query Builder, we are connecting to MySQL data and fetching the categories from categories table manually. Collection Count: Define Page: A condition for Pagination: Fetch Query String: Replace Page: Next Page URL: Convert collection to array: In other frameworks, pagination can be very painful. Laravel's paginator is integrated with the query builder and Eloquent ORM and provides convenient, easy-to-use pagination of database results out of the box. The HTML generated by the paginator is compatible with the Bootstrap CSS framework. Basic Usage. Paginating Query Builder Results


You set it by using the custom pagination..

$query = "Your Query here";

$page = 1;
$perPage = 5;
$query = DB::select($query);
$currentPage = Input::get('page', 1) - 1;
$pagedData = array_slice($query, $currentPage * $perPage, $perPage);
$query =  new Paginator($pagedData, count($query), $perPage);
$query->setPath('Your Url');

$this->data['query'] = $query;

return view('Your_view_file', $this->data, compact('query'));

Here you can specify the path by using the setpath().

In your View

@foreach($query as $rev)
//Contents
@endforeach
<?php echo $Reviews->appends($_REQUEST)->render(); ?>

The appends will append the data.

Thank you.

Laravel Custom Pagination using Raw Query/QueryBuilder , Query Builder: pagination. Page content: forPage; paginate; simplePaginate; getCountForPagination; SQL operators. All example will use same  Laravel Custom Pagination using Raw Query/QueryBuilder For most of the developers, Pagination is a headache. But with Laravel, pagination can be achieved with an inbuilt method called paginate(). It configures limits and offset automatically.


this is the way i did, it use query builder and get the same result with pagination

$paginateNumber = 20;

    $key = $this->removeAccents(strip_tags(trim($request->input('search_key', ''))));
    $package_id = (int)$request->input('package_id', 0);
    $movieHasTrailer = MovieTrailer::select('movie_id')->where('status','!=','-1')->distinct('movie_id')->get();

    $movieIds = array();

    foreach ($movieHasTrailer as $index => $value) {
        $movieIds[] = $value->movie_id;
    }


    $keyparams = array();
    $packages = Package::select('package_name','id')->get();

    $whereClause = [
        ['movie.status', '!=', '-1'],
        ['movie_trailers.status', '!=', '-1']
    ];

    if(!empty($key)){
        $whereClause[] = ['movie.title', 'like', '%'.$key.'%'];
        $keyparams['search_key'] = $key;  
    }

    if($package_id !== 0){
        $whereClause[] = ['movie.package_id', '=', $package_id];
        $keyparams['package_id'] = $package_id;  
    }



    $movies = DB::table('movie')
            ->leftJoin('movie_package','movie.package_id','=','movie_package.id')
            ->leftJoin('movie_trailers','movie.id','=','movie_trailers.movie_id')
            ->where($whereClause)
            ->whereIn('movie.id',$movieIds)
            ->select('movie.*','movie_package.package_name','movie_trailers.movie_id as movie_id', 
                DB::raw('count(*) as total_trailers, movie_id')
            )
            ->groupBy('movie.id')
            ->paginate($paginateNumber);

Laravel 5: Query Builder Helpers – pagination – Jeff's Reference, Laravel 5.3 Pagination With Query Builder Problem [Solved] limit 5 offset 0 [​bindings] => Array ( [0] => %string% [1] => 1,2 ) [time] => 0.46 ) ). In other frameworks, pagination can be very painful. Laravel's paginator is integrated with the query builder and Eloquent ORM and provides convenient, easy-to-use pagination of database results out of the box. The HTML generated by the paginator is compatible with the Bootstrap CSS framework. Basic Usage Paginating Query Builder Results


If you need to hydrate, you can do this...

    $pages = DB::table('stuff')
    ->distinct()
    ->paginate(24, ['stuff.id']);

    $stuffs = Stuff::hydrate($pages->items());

    return view('stuff.index')->with('stuffs', $stuffs)->with('pages', $pages)

$stuffs will contain your model object, $pages will contain your pagination, probably not the most efficient, but it works.

Laravel 5.3 Pagination With Query Builder Problem [Solved], Your best solution is going to be convert this to query builder. ://stackoverflow.​com/questions/44090392/how-to-use-pagination-with-laravel-dbselect-query. Laravel's paginator is integrated with the query builder and Eloquent ORM and provides convenient, easy-to-use pagination of database results out of the box. The HTML generated by the paginator is compatible with the Bootstrap CSS framework.


DB::Select query with pagination ?, Laravel's paginator is integrated with query builder and eloquent query and it provides pagination of database results. Pagination HTML is  Laravel's database query builder provides a convenient, fluent interface to creating and running database queries. It can be used to perform most database operations in your application and works on all supported database systems. The Laravel query builder uses PDO parameter binding to protect your application against SQL injection attacks.


How to paginate query builder results in Laravel 5?, In laravel you can create paginate query by query builder in this way. You can implement in your project by looking at and understanding below. <  The filter query parameters can be used to add where clauses to your Eloquent query. Out of the box we support filtering results by partial attribute value, exact attribute value or even if an attribute value exists in a given array of values.


Pagination Query use in Laravel, Laravel paginator is integrated with the query builder and Eloquent ORM. The paginate method automatically takes care of setting the required limit and the  Query Builder Basic Usage. The database driver plugs right into the original query builder. When using MongoDB connections, you will be able to build fluent queries to perform database operations. For your convenience, there is a collection alias for table as well as some additional MongoDB specific operators/operations.