title: Laravel store data to the pivot table eloquent
body: <p>I am trying to store data to the DB from a form. I have table with columns <code>subject</code>, <code>subjectType(Economic, Informatic subject)</code> and <code>subjectType_subject(pivot table)</code>. I can store data to <code>Subject</code> table but I need to store id's to <code>subjectType_subject</code> table. In my view I have a dropdown nav - Economic subjects and Informatic subjects. If I would like to add Economic subject, I need to add ID's to pivot table to display it. </p>
  
  <p>How can I do this please? </p>
  
  <p>Now I can store data only to Subject table and it doesn't appear on Economic subjects section:</p>
  
  <p><strong>SubjectController</strong></p>
  
  <pre><code>public function create() {
  }
  
  public function store(Request $request)
  {
      $this-&gt;validate($request, [
          'name' =&gt; 'required',
          'abbreviation' =&gt; 'required',
          'description' =&gt; ''
      ]);
  
      $subject = new Subject();
      $subject-&gt;name = $request-&gt;input('name');
      $subject-&gt;abbreviation = $request-&gt;input('abbreviation');
      $subject-&gt;description = $request-&gt;input('description');
  
      $subject-&gt;save();
  
      return redirect()-&gt;back()
           -&gt;with('success', 'Úspěšne jsi vytvořil nový předmět.');
      }
  }
  </code></pre>
  
  <p><strong>Subject Model</strong></p>
  
  <pre><code>public $table = 'Subject';
  
  protected $fillable = [
      'name',
      'abbreviation',
      'description'
  ];
  
  public function subject_type() {
      return $this-&gt;belongsToMany('App\Subject_type',  'subjectType_subject');
  }
  </code></pre>
  
  <p><strong>SubjectType Model</strong></p>
  
  <pre><code>public $table = 'SubjectType';
  
  public function subject() {
      return $this-&gt;belongsToMany('App\Subject', 'subjectType_subject');
  }
  </code></pre>