Created
November 12, 2013 23:40
-
-
Save dadamssg/7440820 to your computer and use it in GitHub Desktop.
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
<?php | |
use Illuminate\Database\Migrations\Migration; | |
class CreateCompaniesTable extends Migration { | |
public function up() | |
{ | |
Schema::create('companies', function($table) | |
{ | |
$table->increments('id'); | |
$table->timestamps(); | |
$table->string('name')->unique(); | |
}); | |
} | |
public function down() | |
{ | |
Schema::drop('companies'); | |
} | |
} | |
class CreatePeopleTable extends Migration { | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('people', function($table) | |
{ | |
$table->increments('id'); | |
$table->timestamps(); | |
$table->string('email')->unique(); | |
$table->string('firstName'); | |
$table->string('lastName'); | |
$table->string('city'); | |
$table->string('state'); | |
$table->integer('company_id'); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::drop('people'); | |
} | |
} | |
class CreateCompaniesView extends Migration { | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
DB::statement("CREATE VIEW companiesView AS | |
select *, | |
( | |
SELECT group_concat(DISTINCT id separator ',') | |
FROM people as p | |
WHERE c.id = p.company_id | |
) as person_ids | |
FROM companies as c"); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
DB::statement("DROP VIEW companiesView"); | |
} | |
} | |
class CompaniesSeeder extends Seeder { | |
public function run() | |
{ | |
DB::table('companies')->delete(); | |
Company::create(['name' => 'Acme Corp']); | |
Company::create(['name' => 'Foo Bar Industries']); | |
Company::create(['name' => 'Bogus Inc']); | |
} | |
} | |
class PeopleSeeder extends Seeder { | |
public function run() | |
{ | |
DB::table('people')->delete(); | |
$bob = [ | |
'firstName' => 'Bob', | |
'lastName' => 'Johnson', | |
'email' => '[email protected]', | |
'city' => 'Austin', | |
'state' => 'TX', | |
'company_id' => 1 | |
]; | |
$allen = [ | |
'firstName' => 'Allen', | |
'lastName' => 'Thompson', | |
'email' => '[email protected]', | |
'city' => 'San Diego', | |
'state' => 'CA', | |
'company_id' => 2 | |
]; | |
$kara = [ | |
'firstName' => 'Kara', | |
'lastName' => 'Smith', | |
'email' => '[email protected]', | |
'city' => 'Dallas', | |
'state' => 'TX', | |
'company_id' => 2 | |
]; | |
$george = [ | |
'firstName' => 'George', | |
'lastName' => 'Washington', | |
'email' => '[email protected]', | |
'city' => 'Washington', | |
'state' => 'DC', | |
'company_id' => 3 | |
]; | |
Person::create($bob); | |
Person::create($allen); | |
Person::create($kara); | |
Person::create($george); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment