Laravel方法paginate不存在
我试图对模型结果进行分页,但得到的结果是“方法分页不存在”。这是我的密码:Laravel方法paginate不存在,laravel,methods,pagination,Laravel,Methods,Pagination,我试图对模型结果进行分页,但得到的结果是“方法分页不存在”。这是我的密码: $user_dispatches = Dispatch::all()->where('user_id', Auth::id())->paginate(10); 我需要获取用户id等于当前已验证用户id的所有记录。无需paginate()方法即可正常工作。您需要删除all(): 当您使用all()时,将从表中获取所有行并获取一个集合。然后你使用集合方法where()(而不是查询生成器方法where()),然后
$user_dispatches = Dispatch::all()->where('user_id', Auth::id())->paginate(10);
我需要获取用户id等于当前已验证用户id的所有记录。无需paginate()方法即可正常工作。您需要删除
all()
:
当您使用
all()
时,将从表中获取所有行并获取一个集合。然后你使用集合方法where()
(而不是查询生成器方法where()
),然后你尝试对集合使用paginate()
方法,但它不存在。扩展了一点Alexey的完美答案:
Dispatch::where('user_id', auth()->user()->id)->paginate(10);
Dispatch::all()
=>返回一个集合
Dispatch::all()->where()
=>返回一个集合
Dispatch::where()
=>返回一个查询
Dispatch::where()->get()
=>返回一个集合
Dispatch::where()->get()->where()
=>返回一个集合
只能在查询
上调用“分页
”,而不能在集合
上调用
是的,对于
查询
和集合
来说,使用where
函数是完全令人困惑的,它们尽可能地接近它们,但事实就是这样。您需要删除方法all()
:
$user_dispatches = Dispatch::where('user_id', Auth::id())->paginate(10);
由于
all()
返回一个Collection
,而paginate()
使用Builder
来使用所有记录和分页,因此需要使用以下代码:
$user_dispatches = Disspath::paginate(8);
您可以创建自己的自定义类:
<?php
namespace App\CustomClasses;
use Illuminate\Container\Container;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
class ColectionPaginate
{
public static function paginate(Collection $results, $pageSize)
{
$page = Paginator::resolveCurrentPage('page');
$total = $results->count();
return self::paginator($results->forPage($page, $pageSize), $total, $pageSize, $page, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => 'page',
]);
}
/**
* Create a new length-aware paginator instance.
*
* @param \Illuminate\Support\Collection $items
* @param int $total
* @param int $perPage
* @param int $currentPage
* @param array $options
* @return \Illuminate\Pagination\LengthAwarePaginator
*/
protected static function paginator($items, $total, $perPage, $currentPage, $options)
{
return Container::getInstance()->makeWith(LengthAwarePaginator::class, compact(
'items', 'total', 'perPage', 'currentPage', 'options'
));
}
}
解释得很好。应标记为正确答案完美解释@Alexy Mezenin
<?php
namespace App\CustomClasses;
use Illuminate\Container\Container;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
class ColectionPaginate
{
public static function paginate(Collection $results, $pageSize)
{
$page = Paginator::resolveCurrentPage('page');
$total = $results->count();
return self::paginator($results->forPage($page, $pageSize), $total, $pageSize, $page, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => 'page',
]);
}
/**
* Create a new length-aware paginator instance.
*
* @param \Illuminate\Support\Collection $items
* @param int $total
* @param int $perPage
* @param int $currentPage
* @param array $options
* @return \Illuminate\Pagination\LengthAwarePaginator
*/
protected static function paginator($items, $total, $perPage, $currentPage, $options)
{
return Container::getInstance()->makeWith(LengthAwarePaginator::class, compact(
'items', 'total', 'perPage', 'currentPage', 'options'
));
}
}
use App\CustomClasses\ColectionPaginate;
...
$result = $query->limit(100)->get();
$paginatedResult = ColectionPaginate::paginate($result, 10);