Laravel:如何正确地分页?

时间:2018-01-11 08:14:32

标签: php laravel laravel-5.5

我将从三个数组中的数据库返回当前用户的消息:$ today,$ year和带有查询的$ pastYear消息:

$today_messages = Inbox::where($message, $user_id)
                                 ->whereDate('created_at', Carbon::today())
                                 ->orderBy('created_at','desc')
                                 ->get();

$year_messages = Inbox::where($message, $user_id)
                                ->whereBetween('created_at', [now()->startOfYear(), Carbon::yesterday()->endOfDay()])
                                ->orderBy('created_at','desc')
                                ->get();

$past_year_messages = Inbox::where($message, $user_id)
                                     ->where('created_at', '<', now()->startOfYear())
                                     ->orderBy('created_at','desc')
                                     ->get();
$messages = [
                       'today' => $today_messages,
                       'year'  => $year_messages,
                       'pastYear' => $past_year_messages
                    ];

我将返回三个数组并使用三个循环foreach()在视图中打印此数组。简单的代码显示今天发送给当前用户的消息:

@if(count($today) > 0)
    @foreach($today as $message)
    <tr @if($message->seen == 0) class="unread"
        @elseif($message->seen == 1) class=""
        @endif
        >
        <td class="inbox-small-cells">
            <div class="checkbox checkbox-default inline-block">
                <input type="checkbox" id="checkbox012"/>
                <label for="checkbox012"></label>
            </div>
            <i class="zmdi zmdi-star inline-block font-16"></i>
        </td>
        <td class="view-message  dont-show"><a href="{{ route('sended'). '/' .encrypt($message->id) }}">{{$message->subject}}</a>@if($message->seen == 0)<span class="label label-warning pull-right">new</span>@endif</td>
        <td class="view-message ">{{$message->messageSender->name}}</td>
        <td class="view-message  text-right">
            <i class="zmdi zmdi-attachment inline-block mr-15 font-16"></i>
            <span  class="time-chat-history inline-block">{{ $message->created_at->format('H:m') }}</span>
        </td>
    </tr>
    @endforeach
@endif

我如何为当前用户消息创建正确的分页?

3 个答案:

答案 0 :(得分:2)

You can do this:

$perPage = 10;
$today = Inbox::where($message, $user_id)->whereDate('created_at', Carbon::today())->latest()->paginate($perPage);
$thisYear = Inbox::where($message, $user_id)->whereBetween('created_at', [now()->startOfYear(), Carbon::yesterday()->endOfDay()])->latest()->paginate($perPage);
$pastYear = Inbox::where($message, $user_id)->where('created_at', '<', now()->startOfYear())->latest()->paginate($perPage);
$maxPages = max($today->lastPage(), $year->lastPage(), $pastYear->lastPage());

Pass these three variables and $maxPages to the view. Then create pagination links manually:

@for ($i = 0; $i < $maxPages $i++)
    <a href="{{ route('some.route') }}?page={{ $i }}">{{ $i }}</a>
    {{ $loop->last ? '' : ' - ' }}
@endfor

If you don't want to create these links manually, you could try to use LenghAwarePaginator with an empty collection or something. But since you have three collections, I'd manually create these links to keep it simple and maintainable.

答案 1 :(得分:0)

你可以在这样的查询中使用内置paginate方法的laravel。

$today_messages = Inbox::where($message, $user_id)
                             ->whereDate('created_at', Carbon::today())
                             ->orderBy('created_at','desc')->paginate(10)

它将在10条记录后自动创建分页 您可以选择任何分页限制,如果您需要将结果添加到您的视图,那么您可以使用自定义代码向视图添加许多选项 像这样

<?php echo $today_messages->render(); ?>
<?php echo $today_messages->total(); ?>

您可以在观看中使用许多其他选项

答案 2 :(得分:0)

我在我的项目中使用Illuminate\Pagination\LengthAwarePaginator在我的laravel项目中创建分页,然后将其用作:

function paginate($items, $perPage)
{
    if(is_array($items)){
        $items = collect($items);
    }

    return new Illuminate\Pagination\LengthAwarePaginator(
        $items->forPage(Illuminate\Pagination\Paginator::resolveCurrentPage() , $perPage),
        $items->count(), $perPage,
        Illuminate\Pagination\Paginator::resolveCurrentPage(),
        ['path' => Illuminate\Pagination\Paginator::resolveCurrentPath()]
    );
}

您可以在https://laravel.com/api/5.5/Illuminate/Pagination/Paginator.html了解有关此API的更多信息(假设您使用的是laravel 5.5)

我希望这会有所帮助。快乐的编码!