Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions database/migrations/task1/2021_11_08_091231_create_tasks_table.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
<?php

use App\Models\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTasksTable extends Migration
{
class CreateTasksTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
public function up() {
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->bigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->foreignIdFor(User::class);
$table->string('name');
$table->timestamps();
});
Expand All @@ -25,10 +22,8 @@ public function up()
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
public function down() {
Schema::dropIfExists('tasks');
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
<?php

use App\Models\Task;
use App\Models\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCommentsTable extends Migration
{
class CreateCommentsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
public function up() {
Schema::create('comments', function (Blueprint $table) {
$table->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();
});
Expand All @@ -27,10 +24,8 @@ public function up()
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
public function down() {
Schema::dropIfExists('comments');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand All @@ -25,10 +22,8 @@ public function up()
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
public function down() {
Schema::dropIfExists('visitors');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand All @@ -25,10 +22,8 @@ public function up()
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
public function down() {
Schema::dropIfExists('products');
}
}
20 changes: 11 additions & 9 deletions database/migrations/task5/2021_11_09_083121_update_users_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand All @@ -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');
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,25 @@
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();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
public function down() {
Schema::dropIfExists('companies');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,26 @@
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();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
public function down() {
Schema::dropIfExists('companies');
}
}
Loading