title: Laravel - Eloquent attach() not working for models
body: <h2>TL;DR</h2>
  
  <hr>
  
  <p>Trying to get three models to interact using eloquent for a rest api. </p>
  
  <ul>
  <li>User - belongsToMany(pulls) </li>
  <li>Pull - belongsToMany(user) &amp;&amp; belongsToMany(boxes) </li>
  <li>Box  - belongsToMany(pulls)</li>
  </ul>
  
  <p>The pull_user table is working perfectly, I can just attach a user after I save a pull. Saving a box works fine but the attach doesn't work/enter anything into the pivot table (I get no errors though).</p>
  
  <h1>The Problem</h1>
  
  <hr>
  
  <p>I can't get a pivot table that associates two of my models together to attach() after a save. I have the three models listed above, the pivot is working for pull_user but not for pull_box even though the save for box is working perfectly. I am able to save a box without an error but the association just never occurs (no error).</p>
  
  <h1>The Code</h1>
  
  <hr>
  
  <p>pull_box.php</p>
  
  <pre><code>class PullBox extends Migration
  {
  
    public function up()
    {
      Schema::create('pull_box', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;timestamps();
          $table-&gt;integer('pull_id');
          $table-&gt;integer('box_id');
  
      });
    }
  
    public function down()
    {
      Schema::dropIfExists('pull_box');
    }
  }
  </code></pre>
  
  <p>Pull.php</p>
  
  <pre><code>class Pull extends Model
  {
      protected $fillable = ['from', 'to', 'runit_id', 'start_time', 'end_time', 'box_count', 'pull_status', 'audit_status', 'status', 'total_quantity', 'accuracy'];
  
      public function users(){
          return $this-&gt;belongsToMany('App\User');
      }
      public function boxes(){
          return $this-&gt;belongsToMany('App\Box');
      }
  }
  </code></pre>
  
  <p>Box.php</p>
  
  <pre><code>class Box extends Model
  {
      protected $fillable = ['user_id','from', 'to', 'runit_id', 'start_time', 'end_time', 'pull_id', 'total_quantity', 'status', 'accuracy'];
  
      public function pulls(){
          return $this-&gt;belongsToMany('App\Pull');
      }
  }
  </code></pre>
  
  <p>BoxController.php</p>
  
  <pre><code>public function store(Request $request)
  {
      $this-&gt;validate($request, [
          'user_id' =&gt; 'required|integer',
          ...
      ]);
  
      $user_id = $request-&gt;input('user_id');
      ...
  
      $box = new Box([
          'user_id' =&gt; $user_id,
          ...
      ]);
  
      $pull = Pull::whereId($pull_id)-&gt;first();
  
      if($box-&gt;save()){
          $pull-&gt;boxes()-&gt;attach($box-&gt;id);
          $box-&gt;view_box = [
              'href' =&gt; 'api/v1/box/' . $box-&gt;id,
              'method' =&gt; 'GET'
          ];
          $message = [
              'msg' =&gt; 'Box created',
              'box' =&gt; $box,
              'pull' =&gt; $pull_id
          ];
  
          return response()-&gt;json($message, 201);
      }
  
      $response = [
          'msg' =&gt; 'Box creation error, contact supervisor',
      ];
      return response()-&gt;json($response, 404);
  }
  </code></pre>
  
  <h1>The Solution</h1>
  
  <hr>
  
  <p>I need to know how I can get this association working. I am going to need to add a new layer in under the pull for Item, but I don't want to move one before I solve this. I think that my problem has to stem from a syntactical/logical error on my part but I can't see it. There are a bunch of questions on SO that are very close to giving me a solution, but after reading them I wasn't able to solve my problem.</p>
  
  <p>Any help is appreciated.</p>