title: Laravel multiple insert in a table
body: <p>I want to create a post and be able to add different categories, so I made this code</p>
  <pre><code>  public function store(Request $request)
  {
    $this-&gt;validate($request,[
      'title' =&gt; 'required',
      'body' =&gt; 'required',
      'name'=&gt;'required'
    ]);
  
    $post = new \App\Post([
        'title' =&gt; $request-&gt;get('title'),
        'body' =&gt; $request-&gt;get('body')
      ]);
       $post-&gt;save();
  
       foreach ($request-&gt;name as $data) {
         $categories = new \App\Category();
         $categories-&gt;name = $request-&gt;get('name');
         //dd($categories);
        $post-&gt;categories()-&gt;save($categories);
       }
        return redirect()-&gt;route('create');
  }
  </code></pre>
  <p>model Post</p>
  <pre><code>public function categories(){return $this-&gt;hasMany(\App\Category::class, 'post_id');}
  </code></pre>
  <p>model Category</p>
  <pre><code>public function something(){return $this-&gt;belongsTo(\App\Solicitud::class, 'post_id');}
  </code></pre>
  <p>HTML</p>
  <pre><code>&lt;form class=&quot;form-horizontal&quot; role=&quot;form&quot; method=&quot;POST&quot; action=&quot;{{ route('store_data') }}&quot;&gt;
                      {{ csrf_field() }}
                      &lt;div class=&quot;form-group{{ $errors-&gt;has('title') ? ' has-error' : '' }}&quot;&gt;
                          &lt;label for=&quot;title&quot; class=&quot;col-md-4 control-label&quot;&gt;title:&lt;/label&gt;
                          &lt;div class=&quot;col-md-6&quot;&gt;
  
                        &lt;input type=&quot;text&quot; name=&quot;title&quot; placeholder=&quot;Cantidad&quot; class=&quot;form-control&quot;  value=&quot;{{ old('title') }}&quot; /&gt;
  
                              @if ($errors-&gt;has('title'))
                                  &lt;span class=&quot;help-block&quot;&gt;
                                      &lt;strong&gt;{{ $errors-&gt;first('title') }}&lt;/strong&gt;
                                  &lt;/span&gt;
                              @endif
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div class=&quot;form-group{{ $errors-&gt;has('body') ? ' has-error' : '' }}&quot;&gt;
                          &lt;label for=&quot;body&quot; class=&quot;col-md-4 control-label&quot;&gt;Body:&lt;/label&gt;
                          &lt;div class=&quot;col-md-6&quot;&gt;
  
                            &lt;input type=&quot;text&quot; name=&quot;body&quot; placeholder=&quot;Cantidad&quot; class=&quot;form-control&quot;  value=&quot;{{ old('body') }}&quot; /&gt;
  
                              @if ($errors-&gt;has('body'))
                                  &lt;span class=&quot;help-block&quot;&gt;
                                      &lt;strong&gt;{{ $errors-&gt;first('body') }}&lt;/strong&gt;
                                  &lt;/span&gt;
                              @endif
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div class=&quot;form-group{{ $errors-&gt;has('name') ? ' has-error' : '' }}&quot;&gt;
                        &lt;table class=&quot;table table-bordered&quot; id=&quot;dynamic_field&quot;&gt;
                          &lt;thead&gt;
                            &lt;tr&gt;
                            &lt;/tr&gt;
                          &lt;/thead&gt;
                          &lt;tbody&gt;
                            &lt;tr&gt;
                              &lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;name[]&quot; placeholder=&quot;Name&quot; class=&quot;form-control name_list&quot;  value=&quot;{{ old('name') }}&quot; /&gt;&lt;/td&gt;
                              &lt;td&gt;&lt;button type=&quot;button&quot; name=&quot;add&quot; id=&quot;add&quot; class=&quot;btn btn-success&quot;&gt; + &lt;/button&gt;&lt;/td&gt;
                            &lt;/tr&gt;
                          &lt;/tbody&gt;
                         &lt;/table&gt;
                      &lt;/div&gt;
                      &lt;div class=&quot;form-group&quot;&gt;
                          &lt;div class=&quot;col-md-6 col-md-offset-4&quot;&gt;
                              &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;
                                  Create
                              &lt;/button&gt;
                          &lt;/div&gt;
                          &lt;div class=&quot;col-md-6 col-md-offset-4&quot;&gt;
                            &lt;a class=&quot;btn btn-info&quot; href=&quot;{{ URL::previous() }}&quot;&gt;back&lt;/a&gt; &lt;br&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                    &lt;/form&gt;
  </code></pre>
  <p>Scritps :</p>
  <pre><code>      &lt;script&gt;
  $(document).ready(function(){
     var i=1;
     $('#add').click(function(){
          i++;
          $('#dynamic_field').append('&lt;tr id=&quot;row'+i+'&quot;&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;name[]&quot; placeholder=&quot;Name&quot; class=&quot;form-control name_list&quot; /&gt;&lt;/td&gt;&lt;td&gt;&lt;button type=&quot;button&quot; name=&quot;remove&quot; id=&quot;'+i+'&quot; class=&quot;btn btn-danger btn_remove&quot;&gt;-&lt;/button&gt;&lt;/td&gt;&lt;/tr&gt;');
     });
     $(document).on('click', '.btn_remove', function(){
          var button_id = $(this).attr(&quot;id&quot;);
          $('#row'+button_id+'').remove();
     });
  
  });
  &lt;/script&gt;
  </code></pre>
  <p>but I get this error. when I submit the  form</p>
  <blockquote>
  <p>Type error: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, integer given, called in C:\xampp\htdocs\proyect\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 681.</p>
  <p>&quot;Type error: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, integer given, called in C:\xampp\htdocs\proyect\vendor\ ▶&quot;</p>
  </blockquote>
  <p>and if press submit with the input empty I get this error</p>
  <blockquote>
  <p>htmlspecialchars() expects parameter 1 to be string, array given (View: C:\xampp\htdocs\proyect\resources\views\create.blade.php)</p>
  </blockquote>
  <p>but if I edit the code, in this way it works but adds only one category.</p>
  <pre><code>$categories = new \App\Category();
         $categories-&gt;name = $request-&gt;get('name');
         //dd($categories);
        $post-&gt;categories()-&gt;save($categories);
  </code></pre>
  <p>I hope I have explained well and thanks :)</p>