简体   繁体   English

Laravel 获取 All with Relationships 和 Paginate 关系数据

[英]Laravel get All with Relationships and Paginate relationship data

I am trying to get all users and their associated (1:n) journals.我正在尝试获取所有用户及其相关的 (1:n) 期刊。 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我真的希望有人可以帮助我

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:然后,您的 for-each 循环应如下所示:

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

After the for-each loop you can just put:在 for-each 循环之后,您可以放置​​:

$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.通过这种方式,您可以将自定义数组发送到您的刀片,循环遍历它,并呈现用户数据和分页日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM