在laravel中通过子查询进行分页

时间:2017-12-28 17:56:43

标签: php mysql laravel pagination laravel-5.4

以下代码将正确输出3个第一个网上商店:

                <ul class="webshop-list">
                    @foreach($webshops as $webshop)
                        <li>
                            {{ $webshop->name }}
                        </li>
                   @endforeach
                </ul>
                {{ $webshops->links() }}

刀片模板:

{{ $category->webshops->links() }}

但是当我在我的刀片模板中插入分页代码时:

Method links does not exist.

我收到以下错误:

{{1}}

我正在关注此处的文档: https://laravel.com/docs/5.4/pagination

3 个答案:

答案 0 :(得分:2)

您可以创建paginator manually,但如果您想保持代码可维护,只需使用两个集合:

$category = Category::where('slug', $slug)->first();
$shops = Webshop::whereHas('categories', function($q) use($category) {
                 $q->where('id', $category->id);
             })
             ->groupBy('id')
             ->orderBy('name')
             ->with(['brands' => function ($query) {
                 $query->where('active', 1)
                       ->where('replace_by', null)
                       ->orderBy('name');
             }])
             ->paginate(3);

答案 1 :(得分:0)

Use

{{ $category->links() }}

$category should be your variable name which you're passing to blade.


Example

If you're passing data like this return view('viewName', ['names' => $names]); then you have to use {{ $names->links() }}


Read this Laravel documentation

答案 2 :(得分:0)

使用simplePaginate函数

CREATE PROCEDURE [dbo].[DeleteRepairInfo]
    @Name nvarchar(MAX),
    @Model nvarchar(MAX)
AS
    DELETE FROM Repair as R
    WHERE R.CarId in (select CarId from Car where Model = @Model) 
    and R.RepairerId in (select RepairerId from Repairer where Name = @Name)        
GO