:::
主內容區域
15-1 刪除測驗
-
編輯路由
/專案/routes/web.php加入 exam 的刪除路由Route::delete('/exam/{exam}', 'ExamController@destroy')->name('exam.destroy'); -
修改
/專案/resources/views/show.blade.php樣板,加入測驗的刪除按鈕:<form action="{{route('exam.destroy', $exam->id)}}" method="POST" style="display:inline"> @csrf @method('delete') <button type="submit" class="btn btn-danger">刪除</button> </form> - 如:

- 接著在控制器中
/專案/app/Http/Controllers/ExamController.php,加入 delete 的方法,我們一樣用delete()刪除即可,也不需要在抓取任何參數,所以,直接刪完回首頁即可。public function destroy(Exam $exam) { $exam->delete(); return redirect()->route('exam.index'); } - 由於我們在migrations中有設定
$table->foreign('exam_id')->references('id')->on('exams')->onDelete('cascade');,所以在刪除測驗,也會順便把相關聯的題目都一併刪除。
15. 刪除題目
