在我的 Laravel 项目中,我在代码的前 3 行中获取数据并将其组合起来并在我的视图页面中使用它,但我使用完整的数据 ->get();我用 拍摄后分页。我的目标是使用控制器中此函数的分页功能从 3 个不同类别中提取数据,并将它们均衡为单个变量。
我的控制器功能
public function allApprovedQuestions(Request $request)
{
$csQuestions = QuestionCheck::where('QUESTION_TYPE', 1)->with('questionsCS')->get();
$daQuestions = QuestionCheck::where('QUESTION_TYPE', 2)->with('questionsDA')->get();
$dyQuestions = QuestionCheck::where('QUESTION_TYPE', 3)->with('questionsDY')->get();
$csCount = $csQuestions->count();
$daCount = $daQuestions->count();
$dyCount = $dyQuestions->count();
$nonPagiinatedQuestions = collect();
foreach ($csQuestions as $csQuestion) {
$nonPagiinatedQuestions->push((object)[
'id' => $csQuestion->questionsCS->SID,
'question' => $csQuestion->questionsCS->SORU,
'type' => $csQuestion->QUESTION_TYPE,
]);
}
foreach ($daQuestions as $daQuestion) {
$nonPagiinatedQuestions->push((object)[
'id' => $daQuestion->questionsDA->SID,
'question' => $daQuestion->SORU1,
'type' => $daQuestion->QUESTION_TYPE,
]);
}
foreach ($dyQuestions as $dyQuestion) {
$nonPagiinatedQuestions->push((object)[
'id' => $dyQuestion->questionsDY->SID,
'question' => $dyQuestion->questionsDY->SORU,
'type' => $dyQuestion->QUESTION_TYPE,
]);
}
$perPage = 2;
$currentPage = request()->input('page', 1);
$currentPageItems = $nonPagiinatedQuestions->slice(($currentPage - 1) * $perPage, $perPage)->all();
$questions = new LengthAwarePaginator($currentPageItems, $nonPagiinatedQuestions->count(), $perPage, $currentPage);
return view('content.approved-questions', compact('questions', 'csCount', 'daCount', 'dyCount'));
}
我的查看代码
<tbody>
@php
$i = 1;
@endphp
@if($questions->isEmpty())
<tr class=" text-center">
<td colspan="4">Soru Yok</td>
</tr>
@else
@foreach ($questions as $question)
<tr>
<td>
{{$i++}}
</td>
<td>
{{$question->question}},
</td>
<td>
@if ($question->type == 1)
Çoktan Seçmeli
@elseif ($question->type == 2)
Doğru - Yanlış
@else
Boşluklu
@endif
</td>
<td>
<form action="{{ route('spesificQuestion')}}" method="post">
@csrf
<input type="hidden" name="question_id" value="{{$question->id}}">
<input type="hidden" name="type" value="{{$question->type}}">
<button class="btn btn-primary">
<i class="bi bi-eye"></i>
</button>
</form>
</td>
</tr>
@endforeach
@endif
</tbody>
</table>
<div class="d-flex justify-content-center">
<nav aria-label="Page navigation example">
<ul class="pagination">
{{-- Previous Page Link --}}
<li class="page-item {{ $questions->onFirstPage() ? 'disabled' : '' }}">
<a class="page-link" href="{{ $questions->previousPageUrl() }}" aria-label="Previous">
<span aria-hidden="true">« Önceki</span>
</a>
</li>
{{-- Pagination Elements --}}
@for ($i = 1; $i <= $questions->lastPage(); $i++)
@if ($i == 1 || $i == $questions->lastPage() || ($i >= $questions->currentPage() - 2 && $i <= $questions->currentPage() + 2))
<li class="page-item {{ $questions->currentPage() == $i ? 'active' : '' }}">
<a class="page-link" href="/all-approved-questions{{ $questions->url($i) }}">{{ $i }}</a>
</li>
@elseif ($i == $questions->currentPage() - 3 || $i == $questions->currentPage() + 3)
<li class="page-item disabled">
<span class="page-link">...</span>
</li>
@endif
@endfor
{{-- Next Page Link --}}
<li class="page-item {{ $questions->hasMorePages() ? '' : 'disabled' }}">
<a class="page-link" href="{{ $questions->nextPageUrl() }}" aria-label="Next">
<span aria-hidden="true">Sonraki »</span>
</a>
</li>
</ul>
</nav>
</div>
whereIn
子句与一组 with
语句一起使用,然后对其进行分页。
$csQuestions = QuestionCheck::whereIn('QUESTION_TYPE', [1, 2, 3])->with([
'questionsCS',
'questionsDA',
'questionsDY',
])->paginate();