title: Laravel 4, how to I create seed data to tables with relationships?
body: <p>I have created a database with two tables, "goals" and "partgoals". The practial use is to make a savings goal (money) and have milestones along the way (partgoals). I want the partgoals obviously be linked to a specific goal. The relationships are created but I run into trouble when trying to create my seed data.</p>
  
  <p>My goal is to set up two goals table like this (GoalsTableSeeder.php):</p>
  
  <pre><code>&lt;?php
  
  class GoalsTableSeeder extends Seeder {
  
      public function run()
      {
          DB::table('goals')-&gt;delete();
  
  
          $goals = array(
              array(
                  'max'      =&gt; 1850000,
                  'avgsav' =&gt; 3500,
                  'duedate'  =&gt; date('2015-03-15'),
                  'created_at' =&gt; new DateTime,
                  'updated_at' =&gt; new DateTime,
              ),
              array(
                  'max'      =&gt; 1100000,
                  'avgsav' =&gt; 5000,
                  'duedate'  =&gt; date('2013-11-15'),
                  'created_at' =&gt; new DateTime,
                  'updated_at' =&gt; new DateTime,
              )
          );
  
          DB::table('goals')-&gt;insert( $goals );
      }
  
  }
  </code></pre>
  
  <p>And my partgoals table like this (PartgoalsTableSeeder.php):</p>
  
  <pre><code>&lt;?php
  
  class PartgoalsTableSeeder extends Seeder {
  
      public function run()
      {
          DB::table('partgoals')-&gt;delete();
  
  
          $partgoals = array(
              array(
                  'id' =&gt; 1,
                  'milestone'      =&gt; 100000,
                  'duedate'  =&gt; date('2014-03-15'),
                  'created_at' =&gt; new DateTime,
                  'updated_at' =&gt; new DateTime,
              ),
              array(
                  'id' =&gt; 1,
                  'milestone'      =&gt; 20000,
                  'duedate'  =&gt; date('2013-06-15'),
                  'created_at' =&gt; new DateTime,
                  'updated_at' =&gt; new DateTime,
              ),
              array(
                  'id' =&gt; 2,
                  'milestone'      =&gt; 400000,
                  'duedate'  =&gt; date('2013-09-15'),
                  'created_at' =&gt; new DateTime,
                  'updated_at' =&gt; new DateTime,
              ),
              array(
                  'id' =&gt; 2,
                  'milestone'      =&gt; 200000,
                  'duedate'  =&gt; date('2014-10-15'),
                  'created_at' =&gt; new DateTime,
                  'updated_at' =&gt; new DateTime,
              )
          );
  
          DB::table('partgoals')-&gt;insert( $partgoals );
      }
  
  }
  </code></pre>
  
  <p>The migration table for "goals":</p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Database\Schema\Blueprint;
  use Illuminate\Database\Migrations\Migration;
  
  class CreateGoalsTable extends Migration {
  
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('goals', function(Blueprint $table)
          {
              $table-&gt;increments('id');
              $table-&gt;integer('max');
              $table-&gt;float('avgsav');
              $table-&gt;date('duedate');
              $table-&gt;timestamps();
          });
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::drop('goals');
      }
  
  }
  </code></pre>
  
  <p>The migration table for partgoals:</p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Database\Schema\Blueprint;
  use Illuminate\Database\Migrations\Migration;
  
  class CreatePartgoalsTable extends Migration {
  
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('partgoals', function(Blueprint $table)
          {
              $table-&gt;foreign('id')
                  -&gt;references('id')-&gt;on('goals')
                  -&gt;onDelete('cascade');
              $table-&gt;increments('id');
              $table-&gt;float('milestone');
              $table-&gt;date('duedate')-&gt;nullable();
              $table-&gt;timestamps();
          });
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::drop('partgoals');
      }
  
  }
  </code></pre>
  
  <p>What am I doing wrong? I am new to Laravel (and Laravel 4).</p>