title: Laravel save dynamic input in database with foreign key
body: <p>I am using Laravel 5.1</p>
  
  <p>I want to save inputs value of dynamic created inputs in my database.</p>
  
  <p>I basically create the dynamic inputs with this line:</p>
  
  <pre><code>...
  for(var i = 1; i&lt;slider.value; i++) {
            +'&lt;div class=\"input-group\"&gt;'
               +'&lt;input name=\"input-chapter-name[]\" name="" type=\"text\"&gt;'
               +'&lt;input name=\"input-chapter-start[]\" name="" type=\"text\"&gt;'
               +'&lt;input name=\"input-chapter-end[]\" name="" type=\"text\"&gt;'
            +'&lt;/div&gt;'
  }
  </code></pre>
  
  <p>My two models look like this:</p>
  
  <pre><code>&lt;?php
      class Chapters extends Model
      {
          protected $table = 'chapters';
          protected $fillable = ['chapter_name' ,'input-chapter-start', 'input-chapter-end'];
  
          public function product()
          {
              return $this-&gt;belongsTo('App\Product');
          }
  
      }
  </code></pre>
  
  <p>
  
  <pre><code>class Product extends Model
  {
      protected $table = 'products';
      protected $fillable = ['email', 'title', 'filename', 'inputMpg', 'number_of_chapters'];
  
      public static $rules = [
          'email'                =&gt; 'required|email|max:50',
          'title'                =&gt; 'required|max:50',
          'filename'             =&gt; 'required|max:50',
          'input_mpg'            =&gt; 'required',
          'number_of_chapters'   =&gt; 'required',
          'input-chapter-name'   =&gt; 'required',
          'input-chapter-start'  =&gt; 'required',
          'input-chapter-end'    =&gt; 'required'
      ];
  
      public function Chapters()
      {
          return $this-&gt;hasMany('App\Chapters');
      }
  
  }
  </code></pre>
  
  <p>And in my Controller I save (try to save) the data like this:</p>
  
  <pre><code> $product-&gt;save();
   $Chapters-&gt;chapters;
   $Chapters-&gt;product()-&gt;associate($product);
   $Chapters-&gt;save();
  </code></pre>
  
  <p>And get the following error:</p>
  
  <blockquote>
    <p>ErrorException in Grammar.php line 118: Argument 1 passed to
    Illuminate\Database\Grammar::parameterize() must be of the type array,
    integer given, called in
    C:\xampp\htdocs\lariApp\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php
    on line 672 and defined</p>
  </blockquote>
  
  <p><strong>Edit:</strong></p>
  
  <p>my current controller looks like this:</p>
  
  <pre><code>&lt;?php
  
  class ProductController extends Controller
  {
  
      protected $request;
  
      public function request(Request $request)
      {
          $this-&gt;request = $request;
      }
  
      public function createProduct(Request $request, $productID)
      {
          $product        = new Product;
  
          $data = $request-&gt;only('email', 'title', 'filename', 'number_of_chapters');
  
          # Validate Input
          $validator = Validator::make($request-&gt;all(), Product::$rules);
  
          # if validation fails return Erros
          if($validator-&gt;fails())
          {
              return Redirect::back()-&gt;withInput()-&gt;withErrors($validator);
          }
  
          $product-&gt;fill($data);
  
          if($product-&gt;save())
          {
              $product = Product::find($productID);
  
              foreach ($request-&gt;input('chapters', []) as $chaptersData) {
                  $chapters = new Chapters($chaptersData);
                  $chapters-&gt;product()-&gt;associate($product);
                  $chapters-&gt;save();
              }
  
              return redirect()-&gt;route('root')-&gt;with('message', 'WIN')-&gt;withInput();
          }
          else
          {
              return redirect()-&gt;route('newProduct')-&gt;with('message', 'Error')-&gt;withInput();
          }
  
      }
  
  }
  </code></pre>
  
  <p>And my route:</p>
  
  <pre><code>Route::post('create/{productID}', ['as' =&gt; 'createProduct', 'uses' =&gt; 'ProductController@createProduct']);
  </code></pre>