Skip to content

Commit

Permalink
ability to choose batch during quiz creation feature created
Browse files Browse the repository at this point in the history
  • Loading branch information
Soumit-Das committed Mar 10, 2024
1 parent aac63e4 commit 13a5ebe
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 5 deletions.
14 changes: 12 additions & 2 deletions app/Http/Controllers/BatchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,20 @@ class BatchController extends Controller
/**
* Display a listing of the resource.
*/
public function index()
public function index(Request $request)
{
//
$batches = Batch::all();

$name = $request->input('name');

if($name == ""){

$batches = Batch::withCount('quizzes')->paginate(3);
}else{

$batches = Batch::withCount('quizzes')->where('name','like','%' . $name . '%')->paginate(3);
}

return view('batches.index',['batches' => $batches]);
}

Expand Down
24 changes: 24 additions & 0 deletions app/Http/Controllers/QuizController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\Batch;
use App\Models\Quiz;


class QuizController extends Controller
{
Expand All @@ -12,6 +16,8 @@ class QuizController extends Controller
public function index()
{
//
$quizzes = Quiz::With(['batch','user'])->orderBy('title','desc')->paginate(3);
return view('quizzes.index',['quizzes' => $quizzes]);
}

/**
Expand All @@ -20,6 +26,8 @@ public function index()
public function create()
{
//
$batches = Batch::all();
return view('quizzes.create' , ['batches' => $batches]);
}

/**
Expand All @@ -28,6 +36,22 @@ public function create()
public function store(Request $request)
{
//
$title = $request->input('title');
$starting = $request->input('starting');
$ending = $request->input('ending');
$duration = $request->input('duration');
$batch_id = $request->input('batch_id');

$quiz = new Quiz;
$quiz->title = $title;
$quiz->starting = $starting;
$quiz->ending = $ending;
$quiz->duration = $duration;
$quiz->batch_id = $batch_id;
$quiz->user_id = Auth::id();
$quiz->save();

return $quiz->id;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function store(Request $request)
public function show(string $id)
{
//
return User::find(1);
return User::find($id);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public function quizzes(): HasMany
return $this->hasMany(Quiz::class);
}



}


Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ public function up(): void
{
Schema::create('quizzes', function (Blueprint $table) {
$table->id();

$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');

$table->unsignedBigInteger('batch_id');
$table->foreign('batch_id')->references('id')->on('batches');

$table->string('title');
$table->dateTime('starting');
$table->dateTime('ending');
Expand Down
2 changes: 1 addition & 1 deletion resources/views/batches/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<div class="p-6 text-gray-900 dark:text-gray-100">

<form method="POST" action="{{ route('batches.store') }}">
@csrf
@csrf
<div class="grid grid-cols-6 gap-6">
<div class="col-span-6 sm:col-span-4">
<label for="name" value="Name">Name</label>
Expand Down
8 changes: 7 additions & 1 deletion resources/views/batches/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 text-gray-900 dark:text-gray-100">

<form>
<input id="name" class="block mt-1 w-full" type="text" name="name" :value="old('name')" required>
</form>

<x-nav-link :href="route('batches.create')">
Create Batch
</x-nav-link>
@foreach ($batches as $batch)
<p><a href="{{ route('batches.show', ['batch' => $batch]) }}">{{ $batch->id }}</a> -- {{ $batch->name }} -- {{$batch->starting}} -- <a href="{{ route('batches.edit', ['batch' => $batch]) }}">Edit </a></p>
<p><a href="{{ route('batches.show', ['batch' => $batch]) }}">{{ $batch->id }}</a> -- {{ $batch->name }} -- {{$batch->starting}} -- {{ $batch->quizzes_count }} --<a href="{{ route('batches.edit', ['batch' => $batch]) }}">Edit </a></p>
@endforeach
{{ $batches->links() }}
</div>
</div>
</div>
Expand Down
4 changes: 4 additions & 0 deletions resources/views/batches/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<h2> ID - {{ $batch->id }}</h2>
<h2> Name - {{ $batch->name }}</h2>
<h2> Starting - {{ $batch->starting }}</h2>
<h2> Quizzes </h2>
@foreach ($batch->quizzes as $quiz)
<p>-- {{$quiz->title }} --</p>
@endforeach
</div>
</div>
</div>
Expand Down
3 changes: 3 additions & 0 deletions resources/views/layouts/navigation.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<x-nav-link :href="route('batches.index')" :active="request()->routeIs('batches.index')">
Batches
</x-nav-link>
<x-nav-link :href="route('quizzes.index')" :active="request()->routeIs('quizzes.index')">
Quizzes
</x-nav-link>
</div>
</div>

Expand Down
51 changes: 51 additions & 0 deletions resources/views/quizzes/create.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">
Create Quizz
</h2>
</x-slot>

<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 text-gray-900 dark:text-gray-100">

<form method="POST" action="{{ route('quizzes.store') }}">
@csrf
<div class="grid grid-cols-6 gap-6">
<div class="col-span-6 sm:col-span-4">
<label for="title" value="title">title</label>
<input id="title" class="block mt-1 w-full" type="text" name="title" :value="old('title')" required>
</div>
<div class="col-span-6 sm:col-span-2">
<label for="starting" value="Starting" >
<input id="starting" class="block mt-1 w-full" type="datetime-local" name="starting" :value="old('starting')" required>
</div>
<div class="col-span-6 sm:col-span-2">
<label for="ending" value="Ending" >
<input id="ending" class="block mt-1 w-full" type="datetime-local" name="ending" :value="old('ending')" required>
</div>
<div class="col-span-6 sm:col-span-4">
<label for="duration" value="Duration">duration</label>
<input id="duration" class="block mt-1 w-full" type="number" name="duration" :value="old('duration')" required>
</div>
<div class="col-span-6 sm:col-span-4">
<select class="block mt-1 w-full" name="batch_id">
@foreach($batches as $batch)
<option value="{{ $batch->id }}"> {{ $batch->name }} </option>
@endforeach
</select>

</div>
<div class="flex col-span-6 justify-end">
<button>
Create Quizz
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</x-app-layout>
23 changes: 23 additions & 0 deletions resources/views/quizzes/index.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">
Quizzes List
</h2>
</x-slot>

<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 text-gray-900 dark:text-gray-100">
<x-nav-link :href="route('quizzes.create')">
Create Quiz
</x-nav-link>
@foreach ($quizzes as $quiz)
<p><a href="{{ route('quizzes.show', ['quiz' => $quiz]) }}">{{ $quiz->id }}</a> -- {{ $quiz->title }} -- {{$quiz->batch->name}} -- {{$quiz->user->name}} --<a href="{{ route('quizzes.edit', ['quiz' => $quiz]) }}">Edit </a></p>
@endforeach
{{ $quizzes->links() }}
</div>
</div>
</div>
</div>
</x-app-layout>

0 comments on commit 13a5ebe

Please sign in to comment.