mirror of
https://github.com/10h30/Test-Laravel-Migrations.git
synced 2026-07-11 19:05:54 +09:00
Task 7 - automatic column value
This commit is contained in:
@@ -87,3 +87,11 @@ Test method `test_duplicate_name()`.
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## Task 7. Automatic Column Value
|
||||||
|
|
||||||
|
Folder `database/migrations/task7` contains a migration for companies table. Edit that migration, so that if someone creates a company without the name, the automatic name would be "My company".
|
||||||
|
|
||||||
|
Test method `test_automatic_value()`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
class CreateCompaniesTable extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
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->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('companies');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -86,4 +86,13 @@ class MigrationsTest extends TestCase
|
|||||||
Company::create(['name' => 'Company One']);
|
Company::create(['name' => 'Company One']);
|
||||||
Company::create(['name' => 'Company One']);
|
Company::create(['name' => 'Company One']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_automatic_value()
|
||||||
|
{
|
||||||
|
Artisan::call('migrate:fresh', ['--path' => '/database/migrations/task7']);
|
||||||
|
|
||||||
|
Company::create([]);
|
||||||
|
$company = Company::first();
|
||||||
|
$this->assertEquals('My company', $company->name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user