使用缓存分页返回集合Laravel

时间:2019-02-10 20:38:54

标签: php laravel

当我将缓存与分页一起使用时,分页返回集合。为什么?我的代码:

    <form method="POST">
    {% csrf_token %}
        <div class="input-group form-group">
            <div class="input-group-prepend">
                <span class="input-group-text"><i class="far fa-id-card"></i></span>
            </div>
            <!--<input type="text" class="form-control" placeholder="Id" name="id" onblur="this.value=(document.getElementById('uno').value+'-'+a">
            -->
            {{ form.id }}
            {{ form.id.errors }}
        </div>                               
        <div class="input-group form-group">
            <div class="input-group-prepend">
                <span class="input-group-text"><i class="far fa-id-card"></i></span>
            </div>    
            <input type="text" class="form-control" placeholder="Id Carrera" name="id_carr" value="{{ carrera.id }}" disabled>                                
            {{ form.id_carr.errores }}
        </div>

        <div class="input-group form-group">
            <div class="input-group-prepend">
                <span class="input-group-text"><i class="far fa-clock"></i></span>
            </div>
            {{ form.id_caba }}
            {{ form.id_caba.errors }}
        </div>

        <div class="input-group form-group">
            <div class="input-group-prepend">
                <span class="input-group-text"><i class="fas fa-list-ol"></i></span>
            </div>
            {{ form.id_jock }}
            {{ form.id_jock.errors }}
        </div>

        <div class="input-group form-group d-none">
            <div class="input-group-prepend">
                <span class="input-group-text"><i class="fas fa-list-ol"></i></span>
            </div>
            <input type="text" class="form-control" placeholder="Numero" name="numero" value="1" disabled>                                
            {{ form.numero.errors }}
        </div>

        <div class="input-group form-group d-none">
            <div class="input-group-prepend">
                <span class="input-group-text"><i class="fas fa-list-ol"></i></span>
            </div>
            <input type="text" class="form-control" placeholder="Posicion" name="posicion" value="0" disabled>                                
            {{ form.posicion.errors }}
        </div>

        <div class="form-group">
            <button class="btn btn-warning w-100" type="submit">Incluir</button>
        </div>
</form> 

当我这样做时:

    $shops = Cache::rememberForever('shops', function() {
        return Shop::active()->paginate(30);
    });

我得到了集合,但没有分页器对象。为什么?

1 个答案:

答案 0 :(得分:0)

我猜在这篇文章中,他们正在考虑类似的情况:

您可以手动创建分页实例。

$page = Input::get('page', 1); // Get the ?page=1 from the url
$perPage = 15; // Number of items per page
$offset = ($page * $perPage) - $perPage;

return new LengthAwarePaginator(
    array_slice($shops->toArray(), $offset, $perPage, true), // Only grab the items we need
    count($shops), // 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
);

在这里您可以找到有关手动创建分页器

的官方Documentation