Online Test - Create Model And Migration #2
Published: 2022-12-23 06:45:07

**Step 1 - Create Model & Migration Lesson**

```
php artisan make:model Lesson -m
```
Please open the migration file, then in the section **function up** change the code to be like this:

```
public function up()
{
	Schema::create('lessons', function (Blueprint $table) {
		$table->id();
		$table->string('title')->unique();
		$table->timestamps();
	});
}
```
Please open the file **app/Models/Lesson.php**, then change the code to be like this:

```
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Lesson extends Model
{
    use HasFactory;

    /**
     * fillable
     *
     * @var array
     */
    protected $fillable = [
        'title',
    ];
}
```

**Step 2 - Create Model & Migration Classroom**
```
php artisan make:model Classroom -m
```

Please open the migration file, then in the section **function up** change the code to be like this:
```
public function up()
{
	Schema::create('classrooms', function (Blueprint $table) {
		$table->id();
		$table->string('title')->unique();
		$table->timestamps();
	});
}
```
Please open the file **app/Models/Classroom.php**, then change the code to be like this:
```
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Classroom extends Model
{
    use HasFactory;

    /**
     * fillable
     *
     * @var array
     */
    protected $fillable = [
        'title',
    ];
}
```

**Step 3 - Create Model & Migration Exam**
```
php artisan make:model Exam -m
```

Please open the migration file, then in the section **function up** change the code to be like this:

```
public function up()
{
    Schema::create('exams', function (Blueprint $table) {
        $table->id();
        $table->string('title');
        $table->foreignId('lesson_id')->references('id')->on('lessons')->cascadeOnDelete();
        $table->foreignId('classroom_id')->references('id')->on('classrooms')->cascadeOnDelete();
        $table->integer('duration');
        $table->text('description');
        $table->enum('random_question', ['Y', 'N'])->default('Y');
        $table->enum('random_answer', ['Y', 'N'])->default('Y');
        $table->enum('show_answer', ['Y', 'N'])->default('N');
        $table->timestamps();
    });
}
```
Please open the file **app/Models/Exam.php**, then change the code to be like this:

```
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Exam extends Model
{
    use HasFactory;

    /**
     * fillable
     *
     * @var array
     */
    protected $fillable = [
        'title',
        'lesson_id',
        'classroom_id',
        'duration',
        'description',
        'random_question',
        'random_answer',
        'show_answer',
    ];

    /**
     * lesson
     *
     * @return void
     */
    public function lesson()
    {
        return $this->belongsTo(Lesson::class);
    }

    /**
     * classroom
     *
     * @return void
     */
    public function classroom()
    {
        return $this->belongsTo(Classroom::class);
    }

    /**
     * questions
     *
     * @return void
     */
    public function questions()
    {
        return $this->hasMany(Question::class)->orderBy('id', 'DESC');
    }
}
```

**Step 4 - Create Model & Migration Student**
```
php artisan make:model Student -m
```

Please open the migration file, then in the section **function up** change the code to be like this:

```
public function up()
{
    Schema::create('students', function (Blueprint $table) {
        $table->id();
        $table->foreignId('classroom_id')->references('id')->on('classrooms')->cascadeOnDelete();
        $table->bigInteger('nisn')->unique();
        $table->string('name');
        $table->string('password');
        $table->enum('gender', ['L', 'P'])->default('L');
        $table->timestamps();
    });
}
```
Please open the file **app/Models/Student.php**, then change the code to be like this:

```
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
    use HasFactory;

    /**
     * fillable
     *
     * @var array
     */
    protected $fillable = [
        'classroom_id',
        'nisn',
        'name',
        'password',
        'gender'
    ];  

    /**
     * classroom
     *
     * @return void
     */
    public function classroom()
    {
        return $this->belongsTo(Classroom::class);
    }
}
```
**Step 5 - Create Model & Migration Question**
```
php artisan make:model Question -m
```

Please open the migration file, then in the section **function up** change the code to be like this:

```
public function up()
{
    Schema::create('questions', function (Blueprint $table) {
        $table->id();
        $table->foreignId('exam_id')->references('id')->on('exams')->cascadeOnDelete();
        $table->text('question');
        $table->text('option_1')->nullable();
        $table->text('option_2')->nullable();
        $table->text('option_3')->nullable();
        $table->text('option_4')->nullable();
        $table->text('option_5')->nullable();
        $table->integer('answer');  
        $table->timestamps();
    });
}
```

Please open the file **app/Models/Question.php**, then change the code to be like this:

```
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Question extends Model
{
    use HasFactory;

    /**
     * fillable
     *
     * @var array
     */
    protected $fillable = [
        'exam_id',
        'question',
        'option_1',
        'option_2',
        'option_3',
        'option_4',
        'option_5',
        'answer',
    ];
}
```

**Step 6 - Create Model & Migration Exam Session**

```
php artisan make:model ExamSession -m
```

Please open the migration file, then in the section **function up** change the code to be like this:

```
public function up()
{
    Schema::create('exam_sessions', function (Blueprint $table) {
        $table->id();
        $table->foreignId('exam_id')->references('id')->on('exams')->cascadeOnDelete();
        $table->string('title');
        $table->dateTime('start_time');
        $table->dateTime('end_time');
        $table->timestamps();
    });
}
```

Please open the file **app/Models/ExamSession.php**, then change the code to be like this:

```
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ExamSession extends Model
{
    use HasFactory;

    /**
     * fillable
     *
     * @var array
     */
    protected $fillable = [
        'exam_id',
        'title',
        'start_time',
        'end_time',
    ];

    /**
     * exam_groups
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function exam_groups()
    {
        return $this->hasMany(ExamGroup::class);
    }

    /**
     * exam
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function exam()
    {
        return $this->belongsTo(Exam::class);
    }
}
```

**Step 7 - Create Model & Migration Exam Group**

```
php artisan make:model ExamGroup -m
```

Please open the migration file, then in the section **function up** change the code to be like this:

```
public function up()
{
    Schema::create('exam_groups', function (Blueprint $table) {
        $table->id();
        $table->foreignId('exam_id')->references('id')->on('exams')->cascadeOnDelete();
        $table->foreignId('exam_session_id')->references('id')->on('exam_sessions')->cascadeOnDelete();
        $table->foreignId('student_id')->references('id')->on('students')->cascadeOnDelete();
        $table->timestamps();
    });
}
```

Please open the file **app/Models/ExamGroup.php**, then change the code to be like this:

```
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ExamGroup extends Model
{
    use HasFactory;

    /**
     * fillable
     *
     * @var array
     */
    protected $fillable = [
        'exam_id',
        'exam_session_id',
        'student_id',
    ];

    /**
     * exam
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function exam()
    {
        return $this->belongsTo(Exam::class);
    }

    /**
     * exam_session
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function exam_session()
    {
        return $this->belongsTo(ExamSession::class);
    }

    /**
     * student
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function student()
    {
        return $this->belongsTo(Student::class);
    }
}
```

**Step 8 - Create Model & Migration Answer**

```
php artisan make:model Answer -m
```

Please open the migration file, then in the section **function up** change the code to be like this:

```
public function up()
{
    Schema::create('answers', function (Blueprint $table) {
        $table->id();
        $table->foreignId('exam_id')->references('id')->on('exams')->cascadeOnDelete();
        $table->foreignId('exam_session_id')->references('id')->on('exam_sessions')->cascadeOnDelete();
        $table->foreignId('question_id')->references('id')->on('questions')->cascadeOnDelete();
        $table->foreignId('student_id')->references('id')->on('students')->cascadeOnDelete();
        $table->integer('question_order');
        $table->string('answer_order');
        $table->integer('answer');
        $table->enum('is_correct', ['Y', 'N'])->default('N');
        $table->timestamps();
    });
}
```

Please open the file **app/Models/Answer.php**, then change the code to be like this:

```
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Answer extends Model
{
    use HasFactory;

    /**
     * fillable
     *
     * @var array
     */
    protected $fillable = [
        'exam_id',
        'exam_session_id',
        'question_id',
        'student_id',
        'question_order',
        'answer_order',
        'answer',
        'is_correct',
    ];

    /**
     * question
     *
     * @return void
     */
    public function question()
    {
        return $this->belongsTo(Question::class);
    }
}
```

**Step 9 - Create Model & Migration Grade**

```
php artisan make:model Grade -m
```

Please open the migration file, then in the section **function up** change the code to be like this:

```
public function up()
{
    Schema::create('grades', function (Blueprint $table) {
        $table->id();
        $table->foreignId('exam_id')->references('id')->on('exams')->cascadeOnDelete();
        $table->foreignId('exam_session_id')->references('id')->on('exam_sessions')->cascadeOnDelete();
        $table->foreignId('student_id')->references('id')->on('students')->cascadeOnDelete();
        $table->integer('duration');
        $table->dateTime('start_time')->nullable();
        $table->dateTime('end_time')->nullable();
        $table->integer('total_correct');
        $table->decimal('grade', 5, 2);
        $table->timestamps();
    });
}
```

Please open the file **app/Models/Grade.php**, then change the code to be like this:

```
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Grade extends Model
{
    use HasFactory;

    /**
     * fillable
     *
     * @var array
     */
    protected $fillable = [
        'exam_id',
        'exam_session_id',
        'student_id',
        'duration',
        'start_time',
        'end_time',
        'total_correct',
        'grade',
    ];
    
    /**
     * exam
     *
     * @return void
     */
    public function exam()
    {
        return $this->belongsTo(Exam::class);
    }

    /**
     * exam_session
     *
     * @return void
     */
    public function exam_session()
    {
        return $this->belongsTo(ExamSession::class);
    }

    /**
     * student
     *
     * @return void
     */
    public function student()
    {
        return $this->belongsTo(Student::class);
    }
}
```

**Step 10 - Running the migration**
After successfully creating the **Model and Migration**, now we will run the migrate command so tah the code tah we created above can be generated into tables, filds and relations.

```
php artisan migrate
```