phplaraveleloquentpaginationlaravel-blade

Laravel get All with Relationships and Paginate relationship data


I am trying to get all users and their associated (1:n) journals. However I want to add pagination to the associated journals, not the users

My Controller:

public function index()
    {
        $users = User::with(['journal'])->orderBy('name', 'asc')->get();
        return view('journals/journals', ['users' => $users]);
    }

My Blade:

 @foreach($users as $user)
                            
                        <a class="list-group-item list-group-item-action" data-toggle="collapse" href="#collapse{{$user->id}}" role="button" aria-expanded="false" aria-controls="collapse{{$user->id}}">
                            {{$user->name}}
                        </a>
                        <div class="collapse mt-1" id="collapse{{$user->id}}">
                            <div class="card card-body">
                                <div class="list-group">
                                <table class="table table-sm table-hover">
                                    <thead>
                                        <tr>
                                            <th scope="col">Kunde</th>
                                            <th scope="col">Betreff</th>
                                            <th scope="col">Leistungsart</th>
                                            <th scope="col">Beginn</th>
                                            <th scope="col">Ende</th>
                                            <th scope="col">Dauer</th>
                                            <th scope="col">Arbeitszeit</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach($user->journal as $journal)
                                        
                                            <tr onclick="window.location='{{route('journal.show', [$journal->id])}}'">
                                            
                                                <th scope="row">{{$journal->customer->name}}</th>
                                                <td>{{$journal->title}}</td>
                                                <td>{{$journal->type}}</td>
                                                <td>{{$journal->started}}</td>
                                                <td>{{$journal->ended}}</td>
                                                <td>{{$journal->duration}}</td>
                                                <td>{{$journal->worked}}</td>
                                            </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                                </div>
                            </div>
                        </div>

                        @endforeach

My Journal Model:

public function user(){
        return $this->belongsTo('App\User', 'user_id', 'id');
    }

My User Model:

public function journal(){
        return $this->hasMany('App\Journal', 'user_id', 'id');
    }

Again, what I am trying to achieve is that I can print out every user and paginate their journals, not paginate the users

I really hope someone can help me there


Solution

  • Render in blade

    Above your journals for-each loop in your blade, try and put something like this:

    @php
    $paginated_journals = $user->journal()->paginate(10);
    @endphp
    

    And then, your for-each loop should look like this:

    @foreach($paginated_journals as $journal)
    ...
    @endforeach
    

    After the for-each loop you can just put:

    $paginated_journals->links()
    

    to get the links for pagination

    Pre-load data in controller

    You can do the same thing server-side. Create a custom array that is empty, go through each user, and add sub array to that custom array:

    array_push($custom_array, ['user' => $user, 'journals' => $user->journal()->paginate(10)])
    

    This way you can send the custom array to your blade, loop through it, and render user data and paginated journals.