-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add models, define relationship, revert 068fe29
* 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
1 parent
aa49007
commit 8274f1f
Showing
4 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
database/migrations/2023_04_20_223654_create_questions_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
database/migrations/2023_04_20_224629_create_hints_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}; |