簡體   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