title: Laravel many to many relationship not working can anyone help me?
body: <p>Would any be so kind as to help me out? 
  I am trying (in vain) to create a many to many relationship in laravel.<br>
  The database structure look good to me but relationship does not work.</p>
  
  <p>I have 2 tables/models <code>users</code> and <code>competencies</code>
  (one user, many users)
  (one competency, many competencies)<br>
  1. fist off I created a database migration:</p>
  
  <pre class="lang-php prettyprint-override"><code>class CreateCompetencyUserTable extends Migration
  {
      public function up()
      {
          Schema::create('competency_user', function (Blueprint $table) {
              $table-&gt;increments('id');
  
              $table-&gt;integer('competency_id')-&gt;unsigned();
              $table-&gt;foreign('competency_id')-&gt;references('id')-&gt;on('competencies');
  
              $table-&gt;integer('user_id')-&gt;unsigned();
              $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users');
          });
      }
  
      public function down()
      {
          Schema::dropIfExists('competency_user');
      }
  }
  </code></pre>
  
  <ol start="2">
  <li>setup 2
  linking the models
  in the <code>user</code> model i create a methode <code>competencies</code> and in the <code>Competency</code> model a methode <code>users</code>.</li>
  </ol>
  
  <p>The documentation doesn't state anything else so this should be it right?</p>
  
  <pre class="lang-php prettyprint-override"><code>class User extends Authenticatable
  {
      use HasApiTokens, Notifiable, SoftDeletes;
  
      protected $casts = [
          'created_at' =&gt; 'datetime',
          'updated_at' =&gt; 'datetime',
          'is_admin' =&gt; 'bool',
          'is_super_admin' =&gt; 'bool'
      ];
  
      protected $fillable = [
          'name', 'email', 'password', 'team_id'
      ];
  
      protected $hidden = [
          'password', 'remember_token', 'is_admin', 'is_super_admin'
      ];
  
      public function competencies()
      {
          return $this-&gt;belongsToMany(Competency::class,'competency_user','user_id','competency_id');
      }
  }
  </code></pre>
  
  <pre class="lang-php prettyprint-override"><code>class Competency extends Model
  {
      use SoftDeletes;
  
      protected $casts = [
          'created_at' =&gt; 'datetime',
          'updated_at' =&gt; 'datetime',
      ];
  
      protected $fillable = [
          'name', 'description', 'icon', 'competency_set_id', 'card_image_da_id', 'order_value'
      ];
  
      public function users()
      {
          return $this-&gt;belongsToMany(User::class, 'competency_user', 'competency_id', 'user_id');
      }
  </code></pre>
  
  <p>after that when i run </p>
  
  <pre class="lang-php prettyprint-override"><code>$user = User::find(1);
  $user-&gt;competencies()-&gt;attach(4);
  $user-&gt;save();
  </code></pre>
  
  <p>or </p>
  
  <pre class="lang-php prettyprint-override"><code>$user = User::find(1);
  $competency = Competency::find(4);
  $user-&gt;competencies()-&gt;attach($competency);
  $user-&gt;save();
  </code></pre>
  
  <p>nothing..... </p>
  
  <p>i hope any one can help me out and that i have provide enough information
  thx 
  Maxim</p>
  
  <p>edit: </p>
  
  <p>migration: users </p>
  
  <pre class="lang-php prettyprint-override"><code>class CreateUsersTable extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('users', function (Blueprint $table) {
              $table-&gt;increments('id');
              $table-&gt;string('name');
              $table-&gt;string('email')-&gt;unique();
              $table-&gt;string('password');
              $table-&gt;boolean('is_admin')-&gt;default(false);
              $table-&gt;rememberToken();
              $table-&gt;timestamps();
          });
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::dropIfExists('users');
      }
  }
  </code></pre>
  
  <p>migration competencies</p>
  
  <pre class="lang-php prettyprint-override"><code>class CreateCompetenciesTable extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('competencies', function (Blueprint $table) {
  
              $table-&gt;increments('id');
              $table-&gt;timestamp('created_at')-&gt;useCurrent();
              $table-&gt;timestamp('updated_at')-&gt;default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
  
              $table-&gt;string('icon');
              $table-&gt;string('name');
              $table-&gt;mediumText('description');
              $table-&gt;unsignedInteger('order_value')-&gt;default(99999);
  
              $table-&gt;unsignedInteger('competency_set_id');
              $table-&gt;foreign('competency_set_id')-&gt;references('id')-&gt;on('competency_sets');
          });
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::dropIfExists('competencies');
      }
  }
  </code></pre>