Skip to content

Commit

Permalink
Add models, define relationship, revert 068fe29
Browse files Browse the repository at this point in the history
* Generate model and migration using:
$ ./vendor/bin/sail artisan make:model -m Question
$ ./vendor/bin/sail artisan make:model -m Hint
* Create foreign key relationship between tables ('question_id' on the 'hints' table references the 'id' column on the 'questions' table)
*     Reverting 068fe29 and using it in combination with the (new) .env file (from PR #7 - ie. with the changed/new password) were required in order for the following to work:

$ ./vendor/bin/sail artisan migrate
  • Loading branch information
hfagerlund committed Apr 23, 2023
1 parent aa49007 commit 8274f1f
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
22 changes: 22 additions & 0 deletions app/Models/Hint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Question;

class Hint extends Model
{
use HasFactory;

protected $fillable = ['question_id', 'hint'];

/**
* Get the question that owns the hint.
*/
public function question()
{
return $this->belongsTo(Question::class);
}
}
21 changes: 21 additions & 0 deletions app/Models/Question.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Hint;

class Question extends Model
{
use HasFactory;

protected $fillable = ['title', 'body'];
/**
* Get the hints for the question.
*/
public function hints()
{
return $this->hasMany(Hint::class);
}
}
29 changes: 29 additions & 0 deletions database/migrations/2023_04_20_223654_create_questions_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('questions', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('body');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('questions');
}
};
32 changes: 32 additions & 0 deletions database/migrations/2023_04_20_224629_create_hints_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('hints', function (Blueprint $table) {
$table->unsignedBigInteger('question_id')->nullable();
$table->text('hint');
$table->timestamps();

//set question_id as foreign key (ie. if question is deleted, its hints will also be deleted)
$table->foreign('question_id')->references('id')->on('questions')
->onDelete('cascade');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('hints');
}
};

0 comments on commit 8274f1f

Please sign in to comment.