Laravel 4 如何在Laravel中对对象数组进行分页?
我正在使用Laravel4.2构建一个应用程序。我有一个用于Laravel 4 如何在Laravel中对对象数组进行分页?,laravel-4,pagination,pivot-table,Laravel 4,Pagination,Pivot Table,我正在使用Laravel4.2构建一个应用程序。我有一个用于单位的模型,另一个用于用户和透视表用户单位。此应用程序中的每个用户都可以选择一个单元并将其添加到其收藏夹列表中,然后可以将此单元及其信息发布为广告 我想选择所有用户发布的所有单位 user\u units(pivot)表包含以下列: id user_id unit_id publish adtype addinfo created_at updated_at 模型上的关系方法
单位
的模型,另一个用于用户
和透视表用户单位
。此应用程序中的每个用户都可以选择一个单元并将其添加到其收藏夹列表中,然后可以将此单元及其信息发布为广告
我想选择所有用户发布的所有单位
user\u units
(pivot)表包含以下列:
id
user_id
unit_id
publish
adtype
addinfo
created_at
updated_at
模型上的关系方法
public function users() {
return $this->belongsToMany('User', 'user_units')
->withPivot('id','publish', 'adtype', 'addinfo');
}
public function units() {
return $this->belongsToMany('Unit', 'user_units')
->withPivot('id','publish', 'adtype', 'addinfo');
}
我的查询选择所有用户发布的所有单位
// Get all published units by users for sale.
$users = User::all();
$publishedSaleUnits = [];
foreach($users as $user){
$userUnits = $user->units()->orderBy('adtype', 'desc')->get();
if(count($userUnits)){
foreach($userUnits as $unit){
if($unit->pivot->publish == 1 && $unit->unit_purpose_id == 1){
if( $unit->pivot->adtype ){
//push all featured ads onto the beginning of array
array_unshift($publishedSaleUnits, $unit);
}else{
//push all normal ads onto the end of array
array_push($publishedSaleUnits, $unit);
}
}
}
}
}
现在我得到了结果,但我不能对结果使用分页,因为它是一个对象数组
那么,有没有更好的解决方案可以通过分页按用户获取所有已发布的单位?您查询数据的方法效率极低。在一个查询中获取数据。嵌套遍历不仅难以读取,而且是性能杀手 要解决分页问题,请执行以下操作: Laravel提供一家Paginator工厂。使用它,您将能够使用自己的数据构建自己的分页器 这很容易
$units = Paginator::make($unit, count($unit), 10);
如果你用的是正面。否则,
illighte\Pagination\Factory
就是您要查找的类。我找到了一个更好的分页数组结果解决方案,并找到了答案
Paginator::make
函数我们只需要传递所需的值,而不是所有值。因为paginator::make
函数只显示发送给它的数据。要将正确的偏移分页数据发送到paginator::make
,应遵循以下方法
$perPage = 5;
$page = Input::get('page', 1);
if ($page > count($publishedSaleUnits) or $page < 1) { $page = 1; }
$offset = ($page * $perPage) - $perPage;
$perPageUnits = array_slice($publishedSaleUnits,$offset,$perPage);
$pagination = Paginator::make($perPageUnits, count($publishedSaleUnits), $perPage);
$perPage=5;
$page=Input::get('page',1);
如果($page>count($publishedSaleUnits)或$page<1){$page=1;}
$offset=($page*$perPage)-$perPage;
$perPageUnits=数组\片($publishedSaleUnits,$offset,$perPage);
$pagination=Paginator::make($perPageUnits,count($publishedSaleUnits),$perPage);
您可以用自己的数组尝试我的代码
$page = isset($request->page) ? $request->page : 1; // Get the page=1 from the url
$perPage = $pagination_num; // Number of items per page
$offset = ($page * $perPage) - $perPage;
$entries = new LengthAwarePaginator(
array_slice($contact_list, $offset, $perPage, true),
count($contact_list), // Total items
$perPage, // Items per page
$page, // Current page
['path' => $request->url(), 'query' => $request->query()] // We
need this so we can keep all old query parameters from the url
);
本文认为,
可以通过创建自定义方法并使用LengthAwarePaginator对数组进行分页
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
class PaginationController extends Controller
{
public function index()
{
$myArray = [
['id'=>1, 'title'=>'Laravel CRUD'],
['id'=>2, 'title'=>'Laravel Ajax CRUD'],
['id'=>3, 'title'=>'Laravel CORS Middleware'],
];
$data = $this->paginate($myArray);
return view('paginate', compact('data'));
}
public function paginate($items, $perPage = 5, $page = null, $options = [])
{
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
$items = $items instanceof Collection ? $items : Collection::make($items);
return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}
}
$units=Paginator::make($units,count($units),10);在完美视图中创建$units->links()之前,我尝试了这个方法,但每个页面都包含来自$units数组的所有结果。我使用paginator::make对$publishedSaleUnits数组中的记录进行分页。但在视图中,我得到了分页链接,但所有链接中都有所有记录。如何将其限制为每页项目。完美答案。