title: How to insert into table with many to many relationship
body: <p>i'm using an excel file to add the data. I'm trying to insert user information along with a user country which is on a second table, which references the country table, something like this: </p>
  
  <p>USERS>USER_COUNTRY>COUNTRIES.  </p>
  
  <p>i'm inserting this data with a controller on laravel and can insert the user information fine but not this extra field.</p>
  
  <p>Depending on what i try to do to insert the user_country it doesn't insert it at all but still inserts the other user information.</p>
  
  <pre class="lang-php prettyprint-override"><code>public function store(Request $request)
  {
      ini_set('max_execution_time', 300);
      $users = $request-&gt;all();
      try {
          DB::beginTransaction();
          foreach ($users as $user) {
              $dbUser = $this-&gt;getUser($user['USERNAME']);
              Log::error($dbUser);
              $dbUser-&gt;name = $user['NOMBRE'];
              $dbUser-&gt;user_name = $user['USERNAME'];
              $dbUser-&gt;email = $user['CORREO'];
              $dbUser-&gt;last_name = $user['APELLIDO'];
              $dbUser-&gt;password = $user['PASSWORD'];
              $this-&gt;isSet('TIPO-USUARIO', $user);
              $user_type_id = $this-&gt;getUserId($user['TIPO-USUARIO']);
              $dbUser-&gt;user_type_id = $user_type_id-&gt;id;
              foreach (explode(',', str_replace(' ', '', $user['PAIS-USUARIO'])) as $c) {
                  $country = $this-&gt;getCountry($user);
                  Log::error($country);
                  $dbUser-&gt;countries()-&gt;attach($user-&gt;countries);
              }
              $dbUser-&gt;save();
          }
          DB::commit();
      } catch (Exception $e) {
          DB::rollBack();
          throw new HttpException(500, 'Sucedio un error importando la información favor intentar de nuevo');
      }
  }
  </code></pre>
  
  <p>the <code>getCountry</code> function:</p>
  
  <pre class="lang-php prettyprint-override"><code>private function getCountry($id)
  {
      return Country::where('id', $id)-&gt;first();
  }
  </code></pre>
  
  <p>these are the tables if they are of any help</p>
  
  <pre class="lang-php prettyprint-override"><code>Schema::create('users', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;string('name');
          $table-&gt;string('user_name')-&gt;unique();
          $table-&gt;string('email')-&gt;unique();
          $table-&gt;string('last_name');
          $table-&gt;string('password', 60);
          $table-&gt;integer('user_type_id')-&gt;unsigned();
          $table-&gt;foreign('user_type_id')-&gt;references('id')-&gt;on('user_types');
          $table-&gt;rememberToken();
          $table-&gt;timestamps();
  });
  Schema::create('countries', function (Blueprint $table){
          $table-&gt;increments('id');
          $table-&gt;string('name');
          $table-&gt;string('currency_symbol');
          $table-&gt;timestamps();
  });
  Schema::create('user_country', function (Blueprint $table){
          $table-&gt;integer('user_id')-&gt;unsigned();
          $table-&gt;integer('country_id')-&gt;unsigned();
          $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users');
          $table-&gt;foreign('country_id')-&gt;references('id')-&gt;on('countries');
  });
  </code></pre>