cowzo's avatar
Level 2

Incorrect pagination results

Hi all, I have a question about the pagination and I hope someone can help me out. Currently I have a single page with filter options. Based on these filters the results are being rendered.

This is my Controller code:

$from = urldecode($request->input('vanaf'));
$to = urldecode($request->input('tot'));
$count = 0;

$partijen = Partij::with(['aanvoertransactie' => function ($q) use ($from, $to) {
                $q->whereBetween('aanvoertransacties.datum', [$from, $to])
                    ->orderBy('datum');
                }, 'afzender'])
                ->with(['afvoertransactie' => function ($q) use ($from, $to) {
                    $q->whereBetween('afvoertransacties.datum', [$from, $to])
                        ->orderBy('datum');
                }, 'afzender'])
            ->paginate(1);

$datesAanvoer = Aanvoertransactie::select()
                ->whereBetween('datum', [$from, $to])
                ->groupBy('datum')
                ->orderBy('datum')
                ->pluck('datum');

$datesAfvoer = Afvoertransactie::select()
                ->whereBetween('datum', [$from, $to])
                ->groupBy('datum')
                ->orderBy('datum')
                ->pluck('datum');

foreach ($partijen as $partij) {
                if (count($partij->afvoertransactie) > 0 || count($partij->aanvoertransactie) > 0) {
                    $count = 1;
                }
            }

View

@if ($count > 0)
        @foreach ($partijen as $partij)
            @if (count($partij->afvoertransactie) > 0 || count($partij->aanvoertransactie) > 0)
                <div class="panel panel-default">
                    <div class="panel-body">
                        <table class="table">
                            <thead>
                            <tr>
                                <th></th>
                                <th>Datum</th>
                                <th>Volgnummer</th>
                                <th>Bonnummer</th>
                                <th>Kenteken</th>
                                <th>Hoeveelheid</th>
                            </tr>
                            </thead>

                            @if (count($partij->aanvoertransactie) > 0)
                                @foreach ($datesAanvoer as $date)
                                    @if(count($partij->aanvoertransactie->where('datum', '=', $date)) > 0)
                                        <tbody>
                                        @foreach ($partij->aanvoertransactie->where('datum', '=', $date) as $aanvoertransactie)
                                            <tr class="success">
                                                <td><i class="fa fa-plus" aria-hidden="true"></i></td>
                                                <td>{{ Carbon\Carbon::parse($date)->format('d-m-Y') }}</td>
                                                <td>{{ $aanvoertransactie->volgnummer }}</td>
                                                <td>{{ $aanvoertransactie->aanvoerbonnummer }}</td>
                                                <td>{{ $aanvoertransactie->aanvoerkenteken }}</td>
                                                <td>{{ number_format($aanvoertransactie->aanvoerhoeveelheid, 2, ',', '.') }}</td>
                                            </tr>
                                        @endforeach
                                        <tr>
                                            <th colspan="5" style="text-align:right;">Dagtotaal:</th>
                                            <th>{{ number_format($partij->aanvoertransactie->where('datum', '=', $date)->sum('aanvoerhoeveelheid'), 2, ',', '.') }}
                                                ton
                                            </th>
                                        </tr>
                                        </tbody>
                                    @endif
                                @endforeach
                            @endif

                            @if (count($partij->afvoertransactie) > 0)
                                @foreach ($datesAfvoer as $date)
                                    @if(count($partij->afvoertransactie->where('datum', '=', $date)) > 0)
                                        <tbody>
                                        @foreach ($partij->afvoertransactie->where('datum', '=', $date) as $afvoertransactie)
                                            <tr class="danger">
                                                <td><i class="fa fa-minus-circle" aria-hidden="true"></i></td>
                                                <td>{{ Carbon\Carbon::parse($date)->format('d-m-Y') }}</td>
                                                <td>{{ $afvoertransactie->volgnummer }}</td>
                                                <td>{{ $afvoertransactie->afvoerbonnummer }}</td>
                                                <td>{{ $afvoertransactie->afvoerkenteken }}</td>
                                                <td>{{ number_format($afvoertransactie->afvoerhoeveelheid, 2, ',', '.') }}</td>
                                            </tr>
                                        @endforeach
                                        <tr>
                                            <th colspan="5" style="text-align:right">Dagtotaal:</th>
                                            <th>{{ number_format($partij->afvoertransactie->where('datum', '=', $date)->sum('afvoerhoeveelheid'), 2, ',', '.') }}
                                                ton
                                            </th>
                                        </tr>
                                        </tbody>
                                    @endif
                                @endforeach
                            @endif
                        </table>
                    </div>
                </div>
            @endif
        @endforeach

    @else
        <div class="panel panel-default">
            <div class="panel-body">
                <h5>Er zijn geen transacties gevonden voor deze filtercriteria.</h5>
            </div>
        </div>
    @endif

    {{ $partijen->appends(['vanaf' => Input::get('vanaf'), 'tot' => Input::get('tot')])->render() }}

The problem I'm facing is the display of multiple pages while the criteria are not met for all. So for example I have 3 items of "Partij" while 1 of those has a "Afvoertransactie" which does meet the filter criteria. I would expect that the Paginator would just render 1 page. Instead it shows for each "Partij" a page, and when visiting those, it will display the else statement with no results.

Should I be creating a custom Paginator ? Thanks for the help!

0 likes
4 replies
jlrdw's avatar

Adjust the apponds array in the controller and pass it to The View, you could possibly use a switch statement to know which appends you will need.

cowzo's avatar
Level 2

@jlrdw thanks for your suggestion. Could you please explain a bit more in detail?

jlrdw's avatar

As example when building up a search query, and after getting the request

protected function getRecords()
{
    //init query
    $query = Company::orderBy('title');

    if (Input::exists('title')) {

        //get form data
        $input     = Input::all();
        $title     = '%'.$input['title'].'%';
        $postCode  = '%'.$input['postCode'].'%';
        $city      = '%'.$input['city'].'%';
        $tel       = '%'.$input['tel'].'%';
        $type_id   = $input['type_id'];
        $active    = $input['active'];

        //do conditions
        if ($title !='') {
            $query->where('title', 'like', $title);
        }

        if ($postCode !='') {
            $query->where('postCode', 'like', $postCode);
        }

        if ($city !='') {
            $query->where('city', 'like', $city);
        }

        if ($tel !='') {
            $query->where('tel', 'like', $tel);
        }

        if ($type_id !='') {
            $query->where('type_id', '=', $type_id);
        }

        if ($active !='') {
            $query->where('active', '=', $active);
        }

    }

    //execute and pass to final variable
    return $query;
}

I had said switch, forgot I had used if statements.

cowzo's avatar
cowzo
OP
Best Answer
Level 2

@jlrdw Thanks! I found the problem was a bit different. I will add the new code for people who might run into the same problem.

$partijen = Partij::whereHas('aanvoertransactie', function ($q) use ($from, $to) {
                    $q->whereBetween('datum', [$from, $to]);
                })
                ->orWhereHas('afvoertransactie', function ($q) use ($from, $to) {
                    $q->whereBetween('datum', [$from, $to])
                        ->orderBy('datum');
                })
                ->with(['aanvoertransactie' => function ($q) use ($from, $to) {
                    $q->whereBetween('aanvoertransacties.datum', [$from, $to])
                        ->orderBy('datum');
                }])
                ->with(['afvoertransactie' => function ($q) use ($from, $to) {
                    $q->whereBetween('afvoertransacties.datum', [$from, $to])
                        ->orderBy('datum');
                }])
                ->with('afzender')
                ->paginate(1);

Please or to participate in this conversation.