From 92eb36b3c94951809079b66a8da94049b1cac02c Mon Sep 17 00:00:00 2001 From: Isro Hidayatulloh Date: Wed, 2 Oct 2024 22:33:51 +0700 Subject: [PATCH 01/10] Task 1: Migrations with Foreign Key --- .../task1/2021_11_08_091231_create_tasks_table.php | 2 +- .../task1/2021_11_08_092943_create_comments_table.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/database/migrations/task1/2021_11_08_091231_create_tasks_table.php b/database/migrations/task1/2021_11_08_091231_create_tasks_table.php index 08bf628f..7e02e7c3 100644 --- a/database/migrations/task1/2021_11_08_091231_create_tasks_table.php +++ b/database/migrations/task1/2021_11_08_091231_create_tasks_table.php @@ -15,7 +15,7 @@ public function up() { Schema::create('tasks', function (Blueprint $table) { $table->id(); - $table->bigInteger('user_id'); + $table->unsignedBigInteger('user_id'); $table->foreign('user_id')->references('id')->on('users'); $table->string('name'); $table->timestamps(); diff --git a/database/migrations/task1/2021_11_08_092943_create_comments_table.php b/database/migrations/task1/2021_11_08_092943_create_comments_table.php index 0378294b..f02a79fc 100644 --- a/database/migrations/task1/2021_11_08_092943_create_comments_table.php +++ b/database/migrations/task1/2021_11_08_092943_create_comments_table.php @@ -15,10 +15,10 @@ public function up() { Schema::create('comments', function (Blueprint $table) { $table->id(); - $table->unsignedInteger('user_id'); + $table->unsignedBigInteger('user_id'); $table->foreign('user_id')->references('id')->on('users'); - $table->unsignedInteger('comment_id'); - $table->foreign('comment_id')->references('id')->on('comments'); + // $table->unsignedBigInteger('comment_id'); + // $table->foreign('comment_id')->references('id')->on('comments'); $table->string('comment_text'); $table->timestamps(); }); From 5e078ade32677532fb4cc6fed7d53b69648a4807 Mon Sep 17 00:00:00 2001 From: Isro Hidayatulloh Date: Wed, 2 Oct 2024 22:36:51 +0700 Subject: [PATCH 02/10] Task 2: Add Column after Another Column --- .../task2/2021_11_09_075928_add_surname_to_users_table.php | 1 + 1 file changed, 1 insertion(+) diff --git a/database/migrations/task2/2021_11_09_075928_add_surname_to_users_table.php b/database/migrations/task2/2021_11_09_075928_add_surname_to_users_table.php index 5a3422a4..e6ff3b38 100644 --- a/database/migrations/task2/2021_11_09_075928_add_surname_to_users_table.php +++ b/database/migrations/task2/2021_11_09_075928_add_surname_to_users_table.php @@ -16,6 +16,7 @@ public function up() Schema::table('users', function (Blueprint $table) { // TASK: Add a string field "surname" which would go after the field "name" // Write code here + $table->string('surname')->after('name'); }); } From 234626256a742b07d906212e10f1c70371638bc7 Mon Sep 17 00:00:00 2001 From: Isro Hidayatulloh Date: Wed, 2 Oct 2024 22:41:26 +0700 Subject: [PATCH 03/10] Task 3: Soft Deletes --- .../migrations/task3/2021_11_09_080955_create_projects_table.php | 1 + 1 file changed, 1 insertion(+) diff --git a/database/migrations/task3/2021_11_09_080955_create_projects_table.php b/database/migrations/task3/2021_11_09_080955_create_projects_table.php index 9dc9d7b5..3cbcc04d 100644 --- a/database/migrations/task3/2021_11_09_080955_create_projects_table.php +++ b/database/migrations/task3/2021_11_09_080955_create_projects_table.php @@ -19,6 +19,7 @@ public function up() $table->timestamps(); // TASK: Add soft deletes column here + $table->softDeletes(); }); } From 0c98ff0ccf831a1fa458c903aaef795ce88d9895 Mon Sep 17 00:00:00 2001 From: Isro Hidayatulloh Date: Wed, 2 Oct 2024 22:43:26 +0700 Subject: [PATCH 04/10] Task 4: Auto-Delete Related Records --- .../task4/2021_11_09_082205_create_products_table.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/migrations/task4/2021_11_09_082205_create_products_table.php b/database/migrations/task4/2021_11_09_082205_create_products_table.php index 78636019..6d48703a 100644 --- a/database/migrations/task4/2021_11_09_082205_create_products_table.php +++ b/database/migrations/task4/2021_11_09_082205_create_products_table.php @@ -16,7 +16,7 @@ public function up() // TASK: Edit this file, so that deleting category would auto-delete its products Schema::create('products', function (Blueprint $table) { $table->id(); - $table->foreignId('category_id')->constrained(); + $table->foreignId('category_id')->constrained()->onDelete('cascade'); $table->string('name'); $table->timestamps(); }); From 9e626c03fc33d5dd6e77ea323aa1bdd0083cbce6 Mon Sep 17 00:00:00 2001 From: Isro Hidayatulloh Date: Wed, 2 Oct 2024 23:02:56 +0700 Subject: [PATCH 05/10] Task 5: Check if Table/Column Exists --- .../2021_11_09_083121_update_users_table.php | 8 +++++--- ...2021_11_09_083225_recreate_users_table.php | 20 ++++++++++--------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/database/migrations/task5/2021_11_09_083121_update_users_table.php b/database/migrations/task5/2021_11_09_083121_update_users_table.php index c10976a5..e794cc2d 100644 --- a/database/migrations/task5/2021_11_09_083121_update_users_table.php +++ b/database/migrations/task5/2021_11_09_083121_update_users_table.php @@ -14,9 +14,11 @@ class UpdateUsersTable extends Migration public function up() { // TASK: add an if-statement in this file to NOT add column if it already exists - Schema::table('users', function (Blueprint $table) { - $table->string('name'); - }); + if (!Schema::hasColumn('users', 'name')) { + Schema::table('users', function (Blueprint $table) { + $table->string('name'); + }); + } } /** diff --git a/database/migrations/task5/2021_11_09_083225_recreate_users_table.php b/database/migrations/task5/2021_11_09_083225_recreate_users_table.php index 6b15a7c6..fac77cb2 100644 --- a/database/migrations/task5/2021_11_09_083225_recreate_users_table.php +++ b/database/migrations/task5/2021_11_09_083225_recreate_users_table.php @@ -14,15 +14,17 @@ class RecreateUsersTable extends Migration public function up() { // TASK: add an if-statement in this file to NOT create table if it already exists - Schema::create('users', function (Blueprint $table) { - $table->id(); - $table->string('name'); - $table->string('email')->unique(); - $table->timestamp('email_verified_at')->nullable(); - $table->string('password'); - $table->rememberToken(); - $table->timestamps(); - }); + if (!Schema::hasTable('users')) { + Schema::create('users', function (Blueprint $table) { + $table->id(); + $table->string('name'); + $table->string('email')->unique(); + $table->timestamp('email_verified_at')->nullable(); + $table->string('password'); + $table->rememberToken(); + $table->timestamps(); + }); + } } /** From 96ee2b001f77b7389cf31349131b55f7fa5de9b2 Mon Sep 17 00:00:00 2001 From: Isro Hidayatulloh Date: Wed, 2 Oct 2024 23:04:07 +0700 Subject: [PATCH 06/10] Task 6: Duplicate Column Value --- .../task6/2021_11_09_083843_create_companies_table.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/migrations/task6/2021_11_09_083843_create_companies_table.php b/database/migrations/task6/2021_11_09_083843_create_companies_table.php index 9554406a..360998bb 100644 --- a/database/migrations/task6/2021_11_09_083843_create_companies_table.php +++ b/database/migrations/task6/2021_11_09_083843_create_companies_table.php @@ -16,7 +16,7 @@ public function up() // TASK: edit this migration so there couldn't be two companies with the same name Schema::create('companies', function (Blueprint $table) { $table->id(); - $table->string('name'); + $table->string('name')->unique(); $table->timestamps(); }); } From f79bedac428a8be754aa51a336b80a3daacd3991 Mon Sep 17 00:00:00 2001 From: Isro Hidayatulloh Date: Wed, 2 Oct 2024 23:05:23 +0700 Subject: [PATCH 07/10] Task 7: Automatic Column Name --- .../task7/2021_11_09_084922_create_new_companies_table.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/migrations/task7/2021_11_09_084922_create_new_companies_table.php b/database/migrations/task7/2021_11_09_084922_create_new_companies_table.php index 868a2422..36bf0166 100644 --- a/database/migrations/task7/2021_11_09_084922_create_new_companies_table.php +++ b/database/migrations/task7/2021_11_09_084922_create_new_companies_table.php @@ -17,7 +17,7 @@ public function up() // its automatic value of name would be "My company" Schema::create('companies', function (Blueprint $table) { $table->id(); - $table->string('name'); + $table->string('name')->default('My company'); $table->timestamps(); }); } From 668125d8855428ca617879e4c410d4328abf2856 Mon Sep 17 00:00:00 2001 From: Isro Hidayatulloh Date: Wed, 2 Oct 2024 23:07:07 +0700 Subject: [PATCH 08/10] Task 8: Rename Table --- .../task8/2021_11_09_085453_rename_companies_table.php | 1 + 1 file changed, 1 insertion(+) diff --git a/database/migrations/task8/2021_11_09_085453_rename_companies_table.php b/database/migrations/task8/2021_11_09_085453_rename_companies_table.php index dc4ae6f2..7468f47e 100644 --- a/database/migrations/task8/2021_11_09_085453_rename_companies_table.php +++ b/database/migrations/task8/2021_11_09_085453_rename_companies_table.php @@ -14,6 +14,7 @@ class RenameCompaniesTable extends Migration public function up() { // TASK: add a migration to rename table "company" into "companies" + Schema::rename('company', 'companies'); } /** From 8d91f0416ffb180a2c18d56a2321eb0569e42454 Mon Sep 17 00:00:00 2001 From: Isro Hidayatulloh Date: Wed, 2 Oct 2024 23:20:01 +0700 Subject: [PATCH 09/10] Task 10: NULL on Foreign Key --- .../task10/2021_11_09_090858_create_visitors_table.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/migrations/task10/2021_11_09_090858_create_visitors_table.php b/database/migrations/task10/2021_11_09_090858_create_visitors_table.php index 7a53968c..08e7516e 100644 --- a/database/migrations/task10/2021_11_09_090858_create_visitors_table.php +++ b/database/migrations/task10/2021_11_09_090858_create_visitors_table.php @@ -16,7 +16,7 @@ public function up() // TASK: edit this migration so country_id would allow NULL values Schema::create('visitors', function (Blueprint $table) { $table->id(); - $table->foreignId('country_id')->constrained(); + $table->foreignId('country_id')->nullable()->constrained(); $table->string('ip_address'); $table->timestamps(); }); From d4313313d4786d131b9a401f69a738a9581330ca Mon Sep 17 00:00:00 2001 From: Isro Hidayatulloh Date: Wed, 2 Oct 2024 23:28:56 +0700 Subject: [PATCH 10/10] Task 9: --- .../task9/2021_11_09_090018_rename_name_in_companies_table.php | 1 + 1 file changed, 1 insertion(+) diff --git a/database/migrations/task9/2021_11_09_090018_rename_name_in_companies_table.php b/database/migrations/task9/2021_11_09_090018_rename_name_in_companies_table.php index f270c9e7..2c14712b 100644 --- a/database/migrations/task9/2021_11_09_090018_rename_name_in_companies_table.php +++ b/database/migrations/task9/2021_11_09_090018_rename_name_in_companies_table.php @@ -16,6 +16,7 @@ public function up() // TASK: write the migration to rename the column "title" into "name" Schema::table('companies', function (Blueprint $table) { // Write code here + $table->renameColumn("title", "name"); }); }