Online Quiz Platform in laravel | Online exam system | Online quiz system in laravel | part- 3
Controller
QuizController.php
<?php
namespace App\Http\Controllers\Exam;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Quizes;
use App\Models\Questions;
class QuizController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$quizes=Quizes::orderBy('id','desc')->paginate(50);
return view('Exam.quiz.create',compact('quizes'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request,[
'quiz_name'=>'required|unique:quizes',
]);
$data=$request->all();
Quizes::create($data);
return redirect()->back()->with('success','Data add successfully');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$data=Quizes::find($id);
$questions=Questions::where('quizes_id',$id)->get();
return view('Exam.quiz.details',compact('data','questions'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
public function status($id)
{
$data=Quizes::find($id);
if($data->status=="1"){
$data->status=0;
}else{
$data->status=1;
}
$data->save();
}
public function AddQuestion($id)
{
$quizId=Quizes::find($id);
return view('Exam.question.add_question',compact('quizId'));
}
}
Models
Questions.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Questions extends Model
{
protected $fillable=['quizes_id','question','answer','status','options','note'];
public function optionsdata()
{
return $this->hasMany(Options::class)->inRandomOrder();
}
public function quizes()
{
return $this->belongsTo(Quizes::class);
}
}
Views/
details.blade.php
@extends('layouts.admin.master')
@section('title','Quiz Details')
@section('content')
<div class="page-header">
<h1>
Quiz Add
<small>
<i class="ace-icon fa fa-angle-double-right"></i>
Quiz for front page </small> <a href="/quizes">Quiz View</a>
</h1>
</div><!-- /.page-header -->
<div class="row">
<div class="col-xs-12">
<!-- PAGE CONTENT BEGINS -->
@include('admin.messages.message')
<h3>{{$data->quiz_name}}</h3>
<hr>
@foreach($questions as $key=>$ques)
<h5> {{$key+1}}. {{$ques->question}}</h5>
<ol class="ul-list" style="list-style-type: lower-alpha;" >
@foreach($ques->optionsdata as $opt)
<li> <input type="radio" {{$opt->option==$ques->answer ? 'checked' : ''}} /> {{$opt->option}} </li>
@endforeach
</ol>
@if($ques->note!="")
<h5 style="color: blue;">Note: {{$ques->note}}</h5>
@endif
@endforeach
<!-- PAGE CONTENT ENDS -->
</div><!-- /.col -->
</div>
@endsection
@section('js')
<script type="text/javascript">
$(document).on('click','.quiz-status',function(){
var id=$(this).attr('data_id');
var url=("{!!url('/')!!}");
$.get(url+'/quiz_status/'+id,function(fb){
alert('Staus Successfully changed');
});
});
</script>
@endsection
No comments