繁体   English   中英

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

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

我正在尝试获取所有用户及其相关的 (1:n) 期刊。 但是我想为相关的期刊而不是用户添加分页

我的控制器:

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

我的刀片:

 @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

我的期刊模型:

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

我的用户模型:

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

同样,我想要实现的是我可以打印出每个用户并对他们的日记进行分页,而不是对用户进行分页

我真的希望有人可以帮助我

在刀片中渲染

在您的刀片中的每个循环的日志上方,尝试放置如下内容:

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

然后,您的 for-each 循环应如下所示:

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

在 for-each 循环之后,您可以放置​​:

$paginated_journals->links()

获取分页链接

在控制器中预加载数据

您可以在服务器端做同样的事情。 创建一个空的自定义数组,遍历每个用户,并将子数组添加到该自定义数组中:

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

通过这种方式,您可以将自定义数组发送到您的刀片,循环遍历它,并呈现用户数据和分页日志。

暂无
暂无

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

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