Laravel 5 Pagination with Query Builder

I am doing Laravel pagination based on the results of my query and will be displayed in my view. I follow 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 am using a query designer, so I think everything should be fine? Here is 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.

thank

+5
source share
6 answers

in the document on this page: http://laravel.com/docs/5.1/pagination we can see that we are not forced to use eloquence.

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

but don't forget to make groupBy in the request because the paginate method uses it.

, , paginate (select($query))

---

paginator:

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

, , $collection->render();

+2
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;
 }
+2

.

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

, setpath().

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

appends .

.

+1

Paginate Eloquent Models. pagination. , , Eloquent

0

This is the way I did, it uses a query builder and gets the same pagination result

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

If you need to moisturize, you can do it ...

    $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, perhaps not the most efficient, but it works.

0
source

All Articles