php Laravel 5 分页与查询生成器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30861378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:07:01  来源:igfitidea点击:

Laravel 5 Pagination with Query Builder

phplaravelpaginationlaravel-5

提问by Hymanhammer013

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/paginationbut I get an error:

我正在根据我的查询结果制作 Laravel 分页并在我的视图中呈现。我正在遵循本指南http://laravel.com/docs/5.1/pagination但出现错误:

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.

但是,当我删除分页(15)时;它工作正常。

Thanks

谢谢

回答by Bouhnosaure

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

在此页面的文档中:http: //laravel.com/docs/5.1/pagination我们可以看到我们并没有被迫使用 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.

但是,请确保不要在查询中创建 groupBy,因为 paginate 方法使用它。

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

在我不确定您是否可以将 paginate 与查询构建器 ( 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();

在您看来只需使用 $collection->render();

回答by Valentim Araújo

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

回答by Suganya Rajasekar

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().

您可以在此处使用setpath().

In your View

在你看来

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

The appendswill append the data.

appends将追加数据。

Thank you.

谢谢你。

回答by Phong

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

回答by Keith Turkowski

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.

$stuffs 将包含您的模型对象, $pages 将包含您的分页,可能不是最有效的,但它有效。