Online Quiz Platform in laravel | Online exam system | Online quiz system in laravel | part- 2

 

Controller 

QuestionController.php

<?php

namespace App\Http\Controllers\Exam;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Quizes;
use App\Models\Questions;
use App\Models\Options;
class QuestionController extends Controller
{
    public function index()
    {
        $questions=Questions::all();
        return view('Exam.question.view',compact('questions'));
    }

   
    
     
    public function store(Request $request)
    {
           $this->validate($request,[
       'question'=>'required|unique:questions,question,NULL,id,quizes_id,'.$request->quizes_id,
        'quizes_id'=>'required',

       ]);

       $data=$request->all();
      $ques= Questions::create($data);
       
       if(count($request->option) > 0) {
        foreach ($request->option as $item=>$v) {
        $datad=array(
          'questions_id'=>$ques->id,
          'option'=>$request->option[$item]
        );
        Options::insert($datad);
      }
      }

       return redirect()->back()->with('success','Data add successfully');
    }

    
    public function show($id)
    {
        //
    }

     
    public function edit($id)
    {
        $data=Questions::find($id);
          $quiz=Quizes::all();
        return view('Exam.question.edit',compact('data','quiz'));
    }

    
    public function update(Request $request, $id)
    {
         $this->validate($request,[
        'quizes_id'=>'required',
        'question'=>'required',
       ]);
        $data=Questions::find($id);
        $new_data=$request->all();
        $data->update($new_data);

         if(count($request->option_id) > 0) {
        foreach ($request->option_id as $item=>$v) {
        $datad=array(
          
          'option'=>$request->option[$item]
           
        );
        $dbazar=Options::where('id',$request->option_id[$item])->first();
        $dbazar->update($datad);
      }
      }

        return redirect()->back()->with('success','Data update successfully');  
    }

    
    public function destroy($id)
    {
        //
    }
}

add_question.blade.php

@extends('layouts.admin.master')
@section('title','Question add')
@section('content')

<div class="page-header">
<h1>
Question  Add
<small>
<i class="ace-icon fa fa-angle-double-right"></i>
Question for front page </small>&nbsp;<a href="/questions">Question  View</a>
</h1>
</div><!-- /.page-header -->
<div class="row">
<div class="col-md-12">
<!-- PAGE CONTENT BEGINS -->
 @include('admin.messages.message')

 <div class="alert alert-danger" style="display: none;"> </div>
<div class="alert alert-success" style="display: none;"><p>Data add Successfully </p></div>

<form class="form-horizontal" role="form" action="/questions" method="POST" >
    {{csrf_field()}}

<div class="space-4"></div>
<div class="form-group">
    <label class="col-sm-2 control-label no-padding-right" for="form-field-2"> Quiz Name </label>
    <div class="col-sm-10">
         
        <select class="form-control" name="quizes_id" required="">
            <option value=" {{$quizId->id}} ">{{$quizId->quiz_name}}</option>
        </select>
       
    </div>
</div>
<div class="form-group">
    <label class="col-sm-2 control-label no-padding-right" for="form-field-2">Question </label>
    <div class="col-sm-10">
        <input type="text" id="form-field-2" placeholder="question" class="form-control" name="question" required="" />
       
    </div>
</div>

 
    
    <div class="col-md-6">

<div class="form-group">
    <label class="col-sm-3 control-label no-padding-right" for="form-field-2">Option 1 </label>
    <div class="col-sm-9">
        <input type="text" id="form-field-2" placeholder="Option" class="form-control" name="option[]" required="" />
       
    </div>
</div>

    
<div class="form-group">
    <label class="col-sm-3 control-label no-padding-right" for="form-field-2">Option 2 </label>
    <div class="col-sm-9">
        <input type="text" id="form-field-2" placeholder="Option" class="form-control" name="option[]" required="" />
       
    </div>
</div>

</div>
    <div class="col-md-6">
    
<div class="form-group">
    <label class="col-sm-3 control-label no-padding-right" for="form-field-2">Option 3 </label>
    <div class="col-sm-9">
        <input type="text" id="form-field-2" placeholder="Option" class="form-control" name="option[]" required="" />
       
    </div>
</div>

    
<div class="form-group">
    <label class="col-sm-3 control-label no-padding-right" for="form-field-2">Option 4 </label>
    <div class="col-sm-9">
        <input type="text" id="form-field-2" placeholder="Option" class="form-control" name="option[]" required="" />
       
    </div>
</div>

 

</div>
    
<div class="form-group">
    <label class="col-sm-2 control-label no-padding-right" for="form-field-2">Right Answer</label>
    <div class="col-sm-10">
        <input type="text" id="form-field-2" placeholder="Right Answer" class="form-control" name="answer" required="" />
       
    </div>
</div>

<div class="form-group">
    <label class="col-sm-2 control-label no-padding-right" for="form-field-2">Note : </label>
    <div class="col-sm-10">
        <input type="text" id="form-field-2" placeholder=" Note " class="form-control" name="note"   />
       
    </div>
</div>

<div class="space-4"></div>
<div class="clearfix form-actions">
    <div class="col-md-offset-3 col-md-9">
        <button class="btn btn-info" type="submit">
            <i class="ace-icon fa fa-check bigger-110"></i>
            Submit
        </button>

        &nbsp; &nbsp; &nbsp;
        <button class="btn" type="reset">
            <i class="ace-icon fa fa-undo bigger-110"></i>
            Reset
        </button>
    </div>
</div>

<div class="hr hr-24"></div>

 
</form>

 <!-- PAGE CONTENT ENDS -->
</div><!-- /.col -->
</div>
@endsection
@section('js')
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $('#addform').on('submit',function(e){
            alert('yes');
            e.preventDefault();
            $.ajax({
                type:"POST",
                url:"/questions",
                data:$('#addform').serialize(),
        success:function(response){
        console.log(response)
    
            $('.alert-success').show()
                },
                error:function(response){
                    console.log(response)
                     if(response.status==422){
 $.each(response.responseJSON.errors, function (key, item)
          {
            $(".alert-danger").append("<li>"+item+"</li>")
          });

        $('.alert-danger').show()
         }else{
        $('.alert-danger').text(response.responseJSON.error)
        $('.alert-danger').show()
                     }
           
                }
            });
        });
    });
</script>



@endsection
 

view.blade.php

@extends('layouts.admin.master')
@section('title','Question add')
@section('content')

<div class="page-header">
<h1>
Question  Add
<small>
<i class="ace-icon fa fa-angle-double-right"></i>
Question for front page </small>&nbsp;<a href="/questions/create">Add new Question </a>
</h1>
</div><!-- /.page-header -->
<div class="row">
<div class="col-md-12">
<!-- PAGE CONTENT BEGINS -->
 @include('admin.messages.message')


<table class="table table-bordered tale-hover" id="datatables">
    <thead>
        <tr>
            <th>SL</th>
            <th>Quiz Name</th>
            <th>Question</th>
            <th>Right Ans</th>
           
            <th>View</th>
            <th>Edit</th>
            <th>Delete</th>
        </tr>
    </thead>
    <tbody>
        @foreach($questions as $key=>$qus)
            <tr>
                <td>{{++$key}}</td>
                <td>{{$qus->quizes->quiz_name}}</td>
                <td>{{$qus->question}}</td>
                <td>{{$qus->answer}}</td>
                <td>View</td>
                <td><a href="/questions/{{$qus->id}}/edit">Edit</a></td>
                <td>
                    {{Form::open(['url'=>'/questions/'.$qus->id,'method'=>'Delete'])}}
                <button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Are you sure delete this?')">Delete</button>
              {{Form::close()}}
                </td>
            </tr>
        @endforeach
    </tbody>
    
</table>



</div>
</div>



 @endsection


edit.blade.php

@extends('layouts.admin.master')
@section('title','Question Update')
@section('content')

<div class="page-header">
<h1>
Question  Update
<small>
<i class="ace-icon fa fa-angle-double-right"></i>
Question for front page </small>&nbsp;<a href="/questions">View Question  </a>
</h1>
</div><!-- /.page-header -->
<div class="row">
<div class="col-md-12">
<!-- PAGE CONTENT BEGINS -->
 @include('admin.messages.message')

     {{Form::open(['url'=>'/questions/'.$data->id ,'method'=>'PATCH','class'=>'form-horizontal'])}}

<div class="space-4"></div>
<div class="form-group">
    <label class="col-sm-2 control-label no-padding-right" for="form-field-2"> Quiz Name </label>
    <div class="col-sm-10">
         
        <select class="form-control" name="quizes_id" required="">
            <option value="{{$data->quizes_id}}">{{$data->quizes->quiz_name}}</option>
            @foreach($quiz as $qzs)
            <option value="{{$qzs->id}}">{{$qzs->quiz_name}}</option>
            @endforeach
        </select>
       
    </div>
</div>
<div class="form-group">
    <label class="col-sm-2 control-label no-padding-right" for="form-field-2">Question </label>
    <div class="col-sm-10">
        <input type="text" id="form-field-2" placeholder="question" class="form-control" name="question" required="" value="{{$data->question}}" />
       
    </div>
</div>

 
    
 

@foreach($data->optionsdata as $key=>$option)
<input type="hidden" name="option_id[]" value="{{$option->id}}">
<div class="form-group">
    <label class="col-sm-2 control-label no-padding-right" for="form-field-2">Option {{++$key}} </label>
    <div class="col-sm-10">
        <input type="text" id="form-field-2" placeholder="Option" class="form-control" name="option[]" required="" value="{{$option->option}}" />
       
    </div>
</div>
@endforeach
 
 

 
    
<div class="form-group">
    <label class="col-sm-2 control-label no-padding-right" for="form-field-2">Right Answer</label>
    <div class="col-sm-10">
        <input type="text" id="form-field-2" placeholder="Right Answer" class="form-control" name="answer" required="" value="{{$data->answer}}" />
       
    </div>
</div>

<div class="form-group">
    <label class="col-sm-2 control-label no-padding-right" for="form-field-2">Note : </label>
    <div class="col-sm-10">
        <input type="text" id="form-field-2" placeholder=" Note " class="form-control" name="note" value="{{$data->note}}"   />
       
    </div>
</div>


<div class="space-4"></div>
<div class="clearfix form-actions">
    <div class="col-md-offset-3 col-md-9">
        <button class="btn btn-info" type="submit">
            <i class="ace-icon fa fa-check bigger-110"></i>
            Update
        </button>

        &nbsp; &nbsp; &nbsp;
        <button class="btn" type="reset">
            <i class="ace-icon fa fa-undo bigger-110"></i>
            Reset
        </button>
    </div>
</div>

<div class="hr hr-24"></div>

 
{{Form::close()}}

 <!-- PAGE CONTENT ENDS -->
</div><!-- /.col -->
</div>




@endsection



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);
    }
}


Options.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Options extends Model
{
    
protected $fillable=['questions_id','option','status'];
}


No comments

Powered by Blogger.