Skip to content

Instantly share code, notes, and snippets.

@dadamssg
Created November 12, 2013 23:40
Show Gist options
  • Save dadamssg/7440820 to your computer and use it in GitHub Desktop.
Save dadamssg/7440820 to your computer and use it in GitHub Desktop.
<?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' => 'bjohnson@gmail.com',
'city' => 'Austin',
'state' => 'TX',
'company_id' => 1
];
$allen = [
'firstName' => 'Allen',
'lastName' => 'Thompson',
'email' => 'athomp@yahoo.com',
'city' => 'San Diego',
'state' => 'CA',
'company_id' => 2
];
$kara = [
'firstName' => 'Kara',
'lastName' => 'Smith',
'email' => 'kparks@aol.com',
'city' => 'Dallas',
'state' => 'TX',
'company_id' => 2
];
$george = [
'firstName' => 'George',
'lastName' => 'Washington',
'email' => 'gWash@hotmail.com',
'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