Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 5.4长度和宽度_Laravel - Fatal编程技术网

Laravel 5.4长度和宽度

Laravel 5.4长度和宽度,laravel,Laravel,我的大脑突然在这个问题上崩溃了。非常感谢任何关心帮助我的人 这是laravel 5.4中的LengthAwarepaginator 这是代码 $collection = []; foreach ($maincategories->merchantCategory as $merchantCat) { foreach ($merchantCat->merchantSubcategory as $merchantSub) {

我的大脑突然在这个问题上崩溃了。非常感谢任何关心帮助我的人

这是laravel 5.4中的LengthAwarepaginator

这是代码

$collection = [];

            foreach ($maincategories->merchantCategory as $merchantCat) {

               foreach ($merchantCat->merchantSubcategory as $merchantSub) {

                   foreach($merchantSub->products as $products){

                        $collection[] = $products;
                   }
               }
            }

            $paginate = new LengthAwarePaginator($collection, count($collection), 10, 1, ['path'=>url('api/products')]);

            dd($paginate);
它显示得非常完美,但问题是项目数是100。这是我所有的项目,我正确地指定了它。我只需要显示10个

基于LengthAwarePaginator构造函数。这是参考资料

public function __construct($items, $total, $perPage, $currentPage = null, array $options = [])
这是屏幕截图


我哪里出错了?TY

手动创建分页器时,您必须自己对结果集进行切片。分页器的第一个参数应该是所需的结果页,而不是整个结果集

从:


手动创建分页器实例时,应手动“切片”传递给分页器的结果数组。如果不确定如何执行此操作,请查看PHP函数

我建议使用
集合
来帮助解决这个问题:

// ...

$collection = collect($collection);

$page = 1;
$perPage = 10;

$paginate = new LengthAwarePaginator(
    $collection->forPage($page, $perPage),
    $collection->count(),
    $perPage,
    $page,
    ['path' => url('api/products')]
);

对于重选择和避免任何多重选择来计算表中的总计,我们无法使用模型分页

use Illuminate\Pagination\LengthAwarePaginator;
在控制器功能中

if(!isset($input["total"])){ //initial request
         $total = //Find the total only one time.
         $request->request->add(['total' => $total]); //Add it in the request so that we can print it in blade 
}else
         $total = $input["total"];  //After initial request

$currentPage = LengthAwarePaginator::resolveCurrentPage(); //page variable from GET or POST
$perPage = 30; //Page Length 
$offset = ($currentPage - 1) * $perPage; //find the offset to pass in query
$specificRecords = /*Collect specific page records in array 
if mysql then Select * from table limit $perPage offset $offset 
if ms sql then OFFSET {$offset} ROWS FETCH NEXT {$perPage} ROWS ONLY */
$records = new LengthAwarePaginator($specificRecords,$total,$perPage,Null,[ "path" => "/pagepath" ]);
在刀片中:

<center>{{$records->appends(Request::except([ 'page','_token' ]))->links()}}</center>
{{$records->appends(Request::except(['page',''u-token'])->links()}

检查页面标签中的页面和总变量,确保已在列表中添加页面:)

谢谢。。。我知道了,这是链接@Rbex那篇文章有点旧了。我已经用一些更新的集合代码(即
forPage()
方法)更新了我的答案。与链接相比,这要容易得多。无论如何,非常感谢你。你是摇滚明星!这真是一个伟大的发现——我花了几个小时在这上面苦苦挣扎。万分感谢@patricusHoly Moly,我已经搜索了很多年了!