Laravel Eloquent 分页不存在错误
Laravel Eloquent Paginate Doesn't Exist Error
我正在尝试在 Laravel blade/view 中设置分页,但显示错误并显示以下消息:
BadMethodCallException
Method Illuminate\Database\Eloquent\Collection::paginate does not exist.
控制器
public function view()
{
$user = Auth::user();
$basic_info = User::find($user->id)->basic_info;
$category = Category::all()->paginate(10);
return view('admin.article.category-view')->with(['user' => $user, 'basic_info' => $basic_info, 'category' => $category]);
}
查看Blade (admin.article.category-view)
<div class="panel-body">
<table class="table table-hover">
<thead>
<tr>
<th>Category Name</th>
</tr>
</thead>
<tbody>
@foreach($category as $cat)
<tr>
<td>{{ $cat->name }}</td>
</tr>
@endforeach
</tbody>
</table>
{{ $category->links() }}
</div>
仅在查询生成器或 Eloquent 查询上使用分页方法,而不是在集合上,如下所示:
public function view()
{
$user = Auth::user();
$basic_info = User::find($user->id)->basic_info;
$category = Category::paginate(10);
return view('admin.article.category-view')->with(['user' => $user, 'basic_info' => $basic_info, 'category' => $category]);
}
您需要删除 all()
:
$category = Category::paginate(10);
当您使用 all()
时,您会从 table 中获取所有行并获取一个集合
您只能在 Query
上调用 "paginate"
,不能在 Collection
上调用。
我正在尝试在 Laravel blade/view 中设置分页,但显示错误并显示以下消息:
BadMethodCallException
Method Illuminate\Database\Eloquent\Collection::paginate does not exist.
控制器
public function view()
{
$user = Auth::user();
$basic_info = User::find($user->id)->basic_info;
$category = Category::all()->paginate(10);
return view('admin.article.category-view')->with(['user' => $user, 'basic_info' => $basic_info, 'category' => $category]);
}
查看Blade (admin.article.category-view)
<div class="panel-body">
<table class="table table-hover">
<thead>
<tr>
<th>Category Name</th>
</tr>
</thead>
<tbody>
@foreach($category as $cat)
<tr>
<td>{{ $cat->name }}</td>
</tr>
@endforeach
</tbody>
</table>
{{ $category->links() }}
</div>
仅在查询生成器或 Eloquent 查询上使用分页方法,而不是在集合上,如下所示:
public function view()
{
$user = Auth::user();
$basic_info = User::find($user->id)->basic_info;
$category = Category::paginate(10);
return view('admin.article.category-view')->with(['user' => $user, 'basic_info' => $basic_info, 'category' => $category]);
}
您需要删除 all()
:
$category = Category::paginate(10);
当您使用 all()
时,您会从 table 中获取所有行并获取一个集合
您只能在 Query
上调用 "paginate"
,不能在 Collection
上调用。