我打印了 50 个问题,是的,它也有效。但需求改变了如何按 topic_id 提问并在 5 页中按 10 x 10 打印它们。好消息。我拿了 topic_id 和问题。但我如何从控制器中的数据库中获取数据。
我的数据库栏=问题
| id | topic_id |question_text|
| ---|:--------:|:-----------:|
| 1 | 5 | question1 |
| 2 | 6 | question2 |
| 3 | 7 | question3 |
topic_id => 请求问题编号
array:9 [▼
"_token" => "N6AJGDf95iQ5WOae3WKibS3yiEdzXJmW7RPXu6sE"
1 => "5"
2 => "3"
3 => "5"
4 => "1"
5 => "3"
6 => "6"
7 => "7"
8 => "8"
]
这是我的旧控制器
$questions = $request->input('questions');
$inquiries = $request->input('inquiries');
$queries = $request->input('queries');
$examinations = $request->input('examinations');
$inquisitions = $request->input('inquisitions');
$except = [];
$questions = Question::inRandomOrder()->limit(10)->get();
$except = array_merge($except, $questions->pluck('id')->toArray());
$inquiries = Question::inRandomOrder()->whereNotIn('id', $except)->limit(10)->get();
$except = array_merge($except, $inquiries->pluck('id')->toArray())
$queries = Question::inRandomOrder()->whereNotIn('id', $except)->limit(10)->get();
$except = array_merge($except, $queries->pluck('id')->toArray());
$examinations = Question::inRandomOrder()->whereNotIn('id', $except)->limit(10)->get();
$except = array_merge($except, $examinations->pluck('id')->toArray());
$inquisitions = Question::inRandomOrder()->whereNotIn('id', $except)->limit(10)->get();
foreach ($questions as &$question) {
$question->options = QuestionsOption::where('question_id', $question->id)->inRandomOrder()->get();
}
return view('exams.create', compact('questions','inquiries','queries','examinations','inquisitions'));
create.blade.php
<form method="post" name="myform" action="{{ route('exams.store') }}">
{{ csrf_field() }}
<div id="account_details">
<div class="panel panel-default">
<div class="panel-heading">
Нэгдүгээр хэсэг
</div>
<?php //dd($questions) ?>
@if(count($questions) > 0)
<div class="panel-body">
<?php $i = 1; ?>
@foreach($questions as $question)
@if ($i > 1) <hr /> @endif
<div class="row">
<div class="col-xs-12 form-group">
<div class="form-group">
<strong>Асуулт {{ $question->id }}.<br/><br/>{!! nl2br($question->question_text) !!}</strong><br/>
<input
type="hidden"
name="questions[{{ $question->id }}]"
value="{{ $question->id }}">
@foreach($question->options as $option)
<br>
<label class="radio-inline">
<input
type="radio"
name="answers[{{ $question->id }}]"
value="{{ $option->id }}">
{{ $option->option }}
</label>
@endforeach
</div>
</div>
</div>
<?php $i++; ?>
@endforeach
</div>
@endif
</div><input type="button" value="Дараагын хуудас" onclick="show_next('account_details','user_details','bar1');">
</div>
<div id="user_details">
<div class="panel panel-default">
<div class="panel-heading">
Хоёрдугаар хэсэг
</div>
<?php //dd($questions) ?>
@if(count($inquiries) > 0)
<div class="panel-body">
<?php $i = 1; ?>
@foreach($inquiries as $question)
@if ($i > 1) <hr /> @endif
<div class="row">
<div class="col-xs-12 form-group">
<div class="form-group">
<strong>Асуулт {{ $question->id }}.<br/><br/>{!! nl2br($question->question_text) !!}</strong><br/>
<input
type="hidden"
name="questions[{{ $question->id }}]"
value="{{ $question->id }}">
@foreach($question->options as $option)
<br>
<label class="radio-inline">
<input
type="radio"
name="answers[{{ $question->id }}]"
value="{{ $option->id }}">
{{ $option->option }}
</label>
@endforeach
</div>
</div>
</div>
<?php $i++; ?>
@endforeach
</div>
@endif
</div>
<input type="button" value="Өмнөх хуудас" onclick="show_prev('account_details','bar1');">
<input type="button" value="Дараагын хуудас" onclick="show_next('user_details','qualification','bar2');">
</div>
<div id="qualification">
<div class="panel panel-default">
<div class="panel-heading">
Гуравдугаар хэсэг
</div>
<?php //dd($questions) ?>
@if(count($queries) > 0)
<div class="panel-body">
<?php $i = 1; ?>
@foreach($queries as $question)
@if ($i > 1) <hr /> @endif
<div class="row">
<div class="col-xs-12 form-group">
<div class="form-group">
<strong>Асуулт {{ $question->id }}.<br/><br/>{!! nl2br($question->question_text) !!}</strong><br/>
<input
type="hidden"
name="questions[{{ $question->id }}]"
value="{{ $question->id }}">
@foreach($question->options as $option)
<br>
<label class="radio-inline">
<input
type="radio"
name="answers[{{ $question->id }}]"
value="{{ $option->id }}">
{{ $option->option }}
</label>
@endforeach
</div>
</div>
</div>
<?php $i++; ?>
@endforeach
</div>
@endif
</div>
<input type="button" value="Өмнөх хуудас" onclick="show_prev('user_details','bar1');">
<input type="button" value="Дараагын хуудас" onclick="show_next('qualification','exort','bar2');">
</div>
<div id="exort">
<div class="panel panel-default">
<div class="panel-heading">
Дөрөвдүгээр хэсэг
</div>
<?php //dd($questions) ?>
@if(count($examinations) > 0)
<div class="panel-body">
<?php $i = 1; ?>
@foreach($examinations as $question)
@if ($i > 1) <hr /> @endif
<div class="row">
<div class="col-xs-12 form-group">
<div class="form-group">
<strong>Асуулт {{ $question->id }}.<br/><br/>{!! nl2br($question->question_text) !!}</strong><br/>
<input
type="hidden"
name="questions[{{ $question->id }}]"
value="{{ $question->id }}">
@foreach($question->options as $option)
<br>
<label class="radio-inline">
<input
type="radio"
name="answers[{{ $question->id }}]"
value="{{ $option->id }}">
{{ $option->option }}
</label>
@endforeach
</div>
</div>
</div>
<?php $i++; ?>
@endforeach
</div>
@endif
</div>
<input type="button" value="Өмнөх хуудас" onclick="show_prev('qualification','bar1');">
<input type="button" value="Дараагын хуудас" onclick="show_next('exort','wex','bar2');">
</div>
<div id="wex">
<div class="panel panel-default">
<div class="panel-heading">
Тавдугаар хэсэг
</div>
<?php //dd($questions) ?>
@if(count($inquisitions) > 0)
<div class="panel-body">
<?php $i = 1; ?>
@foreach($inquisitions as $question)
@if ($i > 1) <hr /> @endif
<div class="row">
<div class="col-xs-12 form-group">
<div class="form-group">
<strong>Асуулт {{ $question->id }}.<br/><br/>{!! nl2br($question->question_text) !!}</strong><br/>
<input
type="hidden"
name="questions[{{ $question->id }}]"
value="{{ $question->id }}">
@foreach($question->options as $option)
<br>
<label class="radio-inline">
<input
type="radio"
name="answers[{{ $question->id }}]"
value="{{ $option->id }}">
{{ $option->option }}
</label>
@endforeach
</div>
</div>
</div>
<?php $i++; ?>
@endforeach
</div>
@endif
</div>
<input type="hidden" name="torol" value="1">
<div class="form-group">
<input type="button" value="Өмнөх хуудас" onclick="show_prev('exort','bar1');">
<button type="submit">Дуусгах</button>
</div>
</div>
</form>
ExamsController@create
就是这样。因为我需要接受问题数组的输入请求。
@foreach($questions as $question)
@if ($i > 1) <hr /> @endif
<div class="row">
<div class="col-xs-12 form-group">
<div class="form-group">
<strong>Асуулт {{ $question->id }}.<br/><br/>{!! nl2br($question->question_text) !!}</strong><br/>
<input
type="hidden"
name="questions[{{ $question->id }}]"
value="{{ $question->id }}">
@foreach($question->options as $option)
<br>
<label class="radio-inline">
<input
type="radio"
name="answers[{{ $question->id }}]"
value="{{ $option->id }}">
{{ $option->option }}
</label>
@endforeach
</div>
</div>
</div>
<?php $i++; ?>
@endforeach
我会这样做:
$questions = Question::with(['options' => function($query) {
$query->inRandomOrder();
}])->inRandomOrder()->paginate(10);
return view('exams.create', compact('questions'));
请记住,这个解决方案理所当然地认为
hasMany
和 question
之间有一个雄辩的 questionOption
关系,称为 options
。
如果您需要有关如何在视图中处理分页的更多信息,请查看官方文档。
编辑
如果您只需要将问题分成五组,您可以这样做:
$questions = Question::with(['options' => function($query) {
$query->inRandomOrder();
}])->inRandomOrder()->limit(50)->get()->slice(5);
return view('exams.create', compact('questions'));
要在视图中显示结果,您可以执行以下操作:
@foreach($questions as $group)
@foreach ($group as $question)
{{$question}}
@foreach($question->options as $option)
{{$option}}
@endforeach
@endforeach
@endforeach
希望这对您有帮助。