我正在打印50个问题,但是它正在运作。但是要求改变了如何通过topic_id提出问题,并在5页中按10×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 @创建
就是这样。因为我需要接受问题数组的输入请求。
@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
答案 0 :(得分:2)
我会这样做:
$questions = Question::with(['options' => function($query) {
$query->inRandomOrder();
}])->inRandomOrder()->paginate(10);
return view('exams.create', compact('questions'));
请记住,此解决方案理所当然地认为您在hasMany
和question
questionOption
之间有一个雄辩的options
关系。
如果您需要有关如何在视图中处理分页的更多信息,请查看official documentation。
修改强>
如果你只需要将你的问题分成五组,你就可以这样做:
$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
希望这会对你有所帮助。