title: Testing a form array with Laravel 5.4 and phpunit
body: <p>What is the correct syntax to post 'AccountApiKey[merchant_id]' in the below example for testing insertion in Laravel 5.4.</p>
  
  <p>In production, the code works fine without any issues, in tests I get the error "table not found" where it is not inserting correctly.  I've asked in IRC but it stumped them so I'm hoping the wonderful community of stackoverflow can assist.</p>
  
  <p>Thanks!</p>
  
  <p>View:</p>
  
  <pre><code>{{-- Merchant ID form input --}}
  &lt;div class="form-group"&gt;
      {!! Form::label('AccountApiKey[merchant_id]', 'Seller ID:') !!}
      {!! Form::text('AccountApiKey[merchant_id]', null, ['class' =&gt; 'form-control']) !!}
  &lt;/div&gt;
  </code></pre>
  
  <p>Test (See below update for complete test): </p>
  
  <pre><code>    $accountApiKey = factory(AccountApiKey::class)-&gt;make([
          'merchant_id' =&gt; 'test'
      ]);
  
  $this-&gt;post($this-&gt;urlToUse(), [
              'name' =&gt; $account-&gt;name,
              'AccountApiKey[merchant_id]' =&gt; $accountApiKey-&gt;merchant_id,
  ]);
  
  
  
      $this-&gt;assertDatabaseHas('account_api_keys', [
          'merchant_id' =&gt; $accountApiKey-&gt;merchant_id,
  ]);
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>   $account-&gt;accountApiKey()-&gt;save(new AccountApiKey($request-&gt;get('AccountApiKey')));
  </code></pre>
  
  <p>Update as per  Sandeesh comment:</p>
  
  <p>Model:</p>
  
  <pre><code>class AccountApiKey extends Model implements Transformable
  {
      use TransformableTrait;
  
      protected $fillable = ['last_modified_user_id', 'merchant_id'];
  
      protected $events = ['saving' =&gt; SettingsUpdated::class];
  
      public function account()
      {
          return $this-&gt;belongsTo('App\Models\Account\Settings\Accounts');
      }
  
  }
  </code></pre>
  
  <p>Complete Test:</p>
  
  <pre><code> class StoreTest extends TestCase implements TestInterface
  {
  
      use DatabaseMigrations;
  
      /**
       * Tests all forms are inserting correct into database
       */
      public function test_inserting_into_database()
      {
          $user1 = $this-&gt;userAndCompany();
  
          $this-&gt;actingAs($user1);
  
          $account = factory(Account::class)-&gt;create([
              'company_id' =&gt; $user1-&gt;company()-&gt;first()-&gt;id,
          ]);
  
          $accountApiKey = factory(Account\AccountApiKey::class)-&gt;make([
              'last_modified_user' =&gt; $user1-&gt;id,
              'account_id' =&gt; $account-&gt;id,
              'merchant_id' =&gt; 'test'
          ]);
  
  
          $this-&gt;post($this-&gt;urlToUse(), [
              'name' =&gt; $account-&gt;name,
              'AccountApiKey[merchant_id]' =&gt; $accountApiKey-&gt;merchant_id,
              ]);
  
          $this-&gt;assertDatabaseHas('accounts', [
              'name' =&gt; $account-&gt;name, //asserts true
          ]);
  
          $this-&gt;assertDatabaseHas('account_api_keys', [
              'merchant_id' =&gt; $accountApiKey-&gt;merchant_id, // asserts false
          ]);
  
      }
  
          /**
       * The url which is under test
       * @return mixed
       */
      function urlToUse()
      {
          return 'account/settings/account';
      }
  
  }
  </code></pre>