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..f580e22a 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 @@ -1,22 +1,19 @@ id(); - $table->bigInteger('user_id'); - $table->foreign('user_id')->references('id')->on('users'); + $table->foreignIdFor(User::class); $table->string('name'); $table->timestamps(); }); @@ -25,10 +22,8 @@ public function up() /** * Reverse the migrations. * - * @return void */ - public function down() - { + public function down() { Schema::dropIfExists('tasks'); } } 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..de69c6c9 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 @@ -1,24 +1,21 @@ id(); - $table->unsignedInteger('user_id'); - $table->foreign('user_id')->references('id')->on('users'); - $table->unsignedInteger('comment_id'); - $table->foreign('comment_id')->references('id')->on('comments'); + $table->foreignIdFor(User::class); + $table->foreignIdFor(Task::class); $table->string('comment_text'); $table->timestamps(); }); @@ -27,10 +24,8 @@ public function up() /** * Reverse the migrations. * - * @return void */ - public function down() - { + public function down() { Schema::dropIfExists('comments'); } } 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..846cd1f6 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 @@ -4,19 +4,16 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class CreateVisitorsTable extends Migration -{ +class CreateVisitorsTable extends Migration { /** * Run the migrations. * - * @return void */ - public function up() - { + 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(); }); @@ -25,10 +22,8 @@ public function up() /** * Reverse the migrations. * - * @return void */ - public function down() - { + public function down() { Schema::dropIfExists('visitors'); } } 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..4fff8962 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 @@ -4,30 +4,26 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class AddSurnameToUsersTable extends Migration -{ +class AddSurnameToUsersTable extends Migration { /** * Run the migrations. * - * @return void */ - public function up() - { + 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'); }); } /** * Reverse the migrations. * - * @return void */ - public function down() - { + public function down() { Schema::table('users', function (Blueprint $table) { - // + $table->removeColumn('surname'); }); } } 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..fceff5b2 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 @@ -4,31 +4,27 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class CreateProjectsTable extends Migration -{ +class CreateProjectsTable extends Migration { /** * Run the migrations. * - * @return void */ - public function up() - { + public function up() { Schema::create('projects', function (Blueprint $table) { $table->id(); $table->string('name'); $table->timestamps(); // TASK: Add soft deletes column here + $table->softDeletes(); }); } /** * Reverse the migrations. * - * @return void */ - public function down() - { + public function down() { Schema::dropIfExists('projects'); } } 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..fbca1ede 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 @@ -4,19 +4,16 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class CreateProductsTable extends Migration -{ +class CreateProductsTable extends Migration { /** * Run the migrations. * - * @return void */ - public function up() - { + 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()->cascadeOnDelete(); $table->string('name'); $table->timestamps(); }); @@ -25,10 +22,8 @@ public function up() /** * Reverse the migrations. * - * @return void */ - public function down() - { + public function down() { Schema::dropIfExists('products'); } } 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..e7449804 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 @@ -4,16 +4,16 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class UpdateUsersTable extends Migration -{ +class UpdateUsersTable extends Migration { /** * Run the migrations. * - * @return void */ - public function up() - { + public function up() { // TASK: add an if-statement in this file to NOT add column if it already exists + if (Schema::hasColumn('users', 'name')) { + return; + } Schema::table('users', function (Blueprint $table) { $table->string('name'); }); @@ -22,10 +22,12 @@ public function up() /** * Reverse the migrations. * - * @return void */ - public function down() - { - // + public function down() { + if (Schema::hasColumn('users', 'name')) { + Schema::table('users', function (Blueprint $table) { + $table->dropColumn('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..e4d570ff 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 @@ -4,16 +4,16 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class RecreateUsersTable extends Migration -{ +class RecreateUsersTable extends Migration { /** * Run the migrations. * - * @return void */ - public function up() - { + public function up() { // TASK: add an if-statement in this file to NOT create table if it already exists + if (Schema::hasTable('users')) { + return; + } Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); @@ -28,10 +28,10 @@ public function up() /** * Reverse the migrations. * - * @return void */ - public function down() - { - // + public function down() { + if (Schema::hasTable('users')) { + Schema::dropIfExists('users'); + } } } 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..2aca9b5b 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 @@ -4,19 +4,16 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class CreateCompaniesTable extends Migration -{ +class CreateCompaniesTable extends Migration { /** * Run the migrations. * - * @return void */ - public function up() - { + 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(); }); } @@ -24,10 +21,8 @@ public function up() /** * Reverse the migrations. * - * @return void */ - public function down() - { + public function down() { Schema::dropIfExists('companies'); } } 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..4d28b48b 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 @@ -4,20 +4,17 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class CreateNewCompaniesTable extends Migration -{ +class CreateNewCompaniesTable extends Migration { /** * Run the migrations. * - * @return void */ - public function up() - { + public function up() { // TASK: edit this migration so that if company is created without the name // 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(); }); } @@ -25,10 +22,8 @@ public function up() /** * Reverse the migrations. * - * @return void */ - public function down() - { + public function down() { Schema::dropIfExists('companies'); } } 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..ace51539 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 @@ -4,25 +4,22 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class RenameCompaniesTable extends Migration -{ +class RenameCompaniesTable extends Migration { /** * Run the migrations. * - * @return void */ - public function up() - { + public function up() { // TASK: add a migration to rename table "company" into "companies" + Schema::rename('company', 'companies'); } /** * Reverse the migrations. * - * @return void */ - public function down() - { - // + public function down() { + // TASK: add a migration to rename table "companies" into "company" + Schema::rename('companies', 'company'); } } 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..49a74304 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 @@ -4,30 +4,25 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class RenameNameInCompaniesTable extends Migration -{ +class RenameNameInCompaniesTable extends Migration { /** * Run the migrations. * - * @return void */ - public function up() - { + 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'); }); } /** * Reverse the migrations. * - * @return void */ - public function down() - { + public function down() { Schema::table('companies', function (Blueprint $table) { - // + $table->renameColumn('name', 'title'); }); } }