title: Laravel insert to 3 related tables
body: <p>Hi guys I would like to know if anyone of you have tried inserting a record into a table in laravel using 3 related tables? Example</p>
  
  <pre><code>// Database Schema for Pharmacy
  Schema::create('pharmacies', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;string('name');
          $table-&gt;text('address');
  });
  
  // Relationship in App\Pharmacy table
  public function pharmacists() {
      return $this-&gt;hasMany('App\Pharmacist');
  
  }
  </code></pre>
  
  <p>Now I have another table</p>
  
  <pre><code>Schema::create('pharmacists', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;integer('pharmacy_id')-&gt;unsigned()-&gt;index();
          $table-&gt;foreign('pharmacy_id')-&gt;references('id')-&gt;on('pharmacies')-&gt;onDelete('cascade');
          $table-&gt;string('fname');
  });
  
  // And this is the relationship in App\Pharmacist class
  public function account() {
      return $this-&gt;hasOne('App\Account');
  }
  
  public function pharmacy() {
      return $this-&gt;belongsTo('App\Pharmacy');
  }
  </code></pre>
  
  <p>And now for the third table</p>
  
  <pre><code>// This contain the foreign key for pharmacist_id
  Schema::create('accounts', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;integer('pharmacist_id')-&gt;unsigned()-&gt;index();
          $table-&gt;foreign('pharmacist_id')-&gt;references('id')-&gt;on('pharmacists')-&gt;onDelete('cascade');
          $table-&gt;string('username')-&gt;unique();
  });
  
  // This is the relationship in App\Account class
  public function pharmacists() {
      return $this-&gt;belongsTo('App\Pharmacist');
  }
  </code></pre>
  
  <p>Now I tried saving this using this code under AccountsController</p>
  
  <pre><code>$input = Request::all();
  
      $pharmacy = Pharmacy::create([
                      "name" =&gt; "Wendies Chicken",
                      "address" =&gt; "My Address",                        
                  ]);
  
      $pharmacists = new Pharmacist([
          "fname" =&gt; "Administrator",            
      ]);
  
      $account = new Account([
          "username" =&gt; "root",            
      ]);
  
      $pharmacists-&gt;account()-&gt;save($account);
      $pharmacy-&gt;pharmacists()-&gt;save($pharmacists);
  </code></pre>
  
  <p>But I get an error saying </p>
  
  <pre><code>Integrity constraint violation: 1048 Column 'pharmacist_id' cannot be null (SQL: insert into `accounts` (`username`, `pharmacist_id`, `updated_at`, `created_at`) values (root, 2015-08-09 05:13:31, 2015-08-09 05:13:31))
  </code></pre>
  
  <p>Dont know how to save this is just one saving. I would like to save the records in the 3 related tables. Can someone help me with this. Thanks</p>