/專案/app/Http/Controllers/ExamController.php
控制器,修改原本的show()
public function show(Exam $exam)
{
$user = Auth::user();
if ($user and $user->can('進行測驗')) {
$exam->topics = $exam->topics->random(10);
}
return view('exam.show', compact('exam'));
}
$exam->topics
會是一個集合($exam
此時是Exam
的資料物件),我們利用random(10)
來從題目集合中,隨機取10筆來呈現。/專案/resources/views/exam/show.blade.php
樣板,一樣依據權限來呈現不同畫面,不過因為程式碼越來越長,所以,我們可以把一些表單獨立成另外的視圖檔案:
\專案\resources\views\exam\form.blade.php
@if(isset($topic))
{{ bs()->openForm('patch', "/topic/{$topic->id}", ['model' => $topic]) }}
@else
{{ bs()->openForm('post', '/topic') }}
@endif
{{ bs()->formGroup()
->label('題目內容', false, 'text-sm-right')
->control(bs()->textarea('topic')->placeholder('請輸入題目內容'))
->showAsRow() }}
{{ bs()->formGroup()
->label('選項1', false, 'text-sm-right')
->control(bs()->text('opt1')->placeholder('輸入選項1'))
->showAsRow() }}
{{ bs()->formGroup()
->label('選項2', false, 'text-sm-right')
->control(bs()->text('opt2')->placeholder('輸入選項2'))
->showAsRow() }}
{{ bs()->formGroup()
->label('選項3', false, 'text-sm-right')
->control(bs()->text('opt3')->placeholder('輸入選項3'))
->showAsRow() }}
{{ bs()->formGroup()
->label('選項4', false, 'text-sm-right')
->control(bs()->text('opt4')->placeholder('輸入選項4'))
->showAsRow() }}
{{ bs()->formGroup()
->label('正確解答', false, 'text-sm-right')
->control(bs()->select('ans',[1=>1, 2=>2, 3=>3, 4=>4])->placeholder('請設定正確解答'))
->showAsRow() }}
{{ bs()->hidden('exam_id', $exam->id) }}
{{ bs()->formGroup()
->label('')
->control(bs()->submit('儲存'))
->showAsRow() }}
{{ bs()->closeForm() }}
/專案/resources/views/exam/show.blade.php
樣板,利用@include()
引入該檔案,位置放在exam\form.blade.php
,引入時需寫成exam.form
@can('建立測驗')
@include('exam.form')
@endcan
同樣的,我們把題目的呈現也獨立成一個視圖檔案\專案\resources\views\exam\topic.blade.php
<dl>
@forelse ($exam->topics as $key => $topic)
<dt>
<h3>
@can('建立測驗')
<button type="button" class="btn btn-danger btn-del-topic" data-id="{{ $topic->id }}">刪除</button>
<a href="{{route('topic.edit', $topic->id)}}" class="btn btn-warning">編輯</a>
({{$topic->ans}})
@endcan
{{ bs()->badge()->text($key+1) }}
{{$topic->topic}}
</h3>
</dt>
<dd>
{{ bs()->radioGroup("ans[$topic->id]", [
1=>"<span class='opt'>❶ $topic->opt1</span>",
2=>"<span class='opt'>❷ $topic->opt2</span>",
3=>"<span class='opt'>❸ $topic->opt3</span>",
4=>"<span class='opt'>❹ $topic->opt4</span>"
])->addRadioClass(['mx-3']) }}
</dd>
@empty
<div class="alert alert-danger">尚無任何題目</div>
@endforelse
</dl>
再修改/專案/resources/views/exam/show.blade.php
樣板,一樣利用@include()
引入該檔案,位置放在exam\topic.blade.php
,引入時需寫成exam.topic
@include('exam.topic')
@if(Auth::check('建立測驗') || Auth::check('進行測驗'))
@can('進行測驗')
{{ bs()->openForm('post', '/test') }}
@include('exam.topic')
{{ bs()->hidden('user_id', Auth::id()) }}
{{ bs()->hidden('exam_id', $exam->id) }}
<div class="text-center my-5">
{{ bs()->submit('寫完送出') }}
</div>
{{ bs()->closeForm() }}
@else
@include('exam.topic')
@endcan
@else
@component('bs::alert', ['type' => 'info'])
共 {{ $exam->topics->count() }} 題
@endcomponent
@endif
@can
無法使用,故改用Auth::check('建立測驗')
搭配@if
就可以達成。Auth::id()
來抓取即可。$exam->topics
本身是一個集合,要算數量可以用count()
class="alert"
來做,也可以用@component
的方式來做。如果只是簡單的訊息,其實用單純HTML語法來做更簡單。