title: Laravel many-to-many relation with custom table names and IDs
body: <p>Hello so I have many to many relation between Question [table name: <strong>tblquestion</strong>, id: <strong>que_id</strong>] and Agecategory [table name: <strong>tblagecategory</strong>, id: <strong>aca_id</strong>]. They have shared table named QuestionAgecategory [table name: <strong>tblquestionagecategory</strong>, id: <strong>qac_id</strong>]. </p>
  
  <p><em>I want to note that all the IDS and table names are custom named and not according to typical Laravel syntax.</em></p>
  
  <p>And I am trying to relate them in Laravel. So far it returns null when I try to look $question->agecategories;</p>
  
  <blockquote>
    <blockquote>
      <blockquote>
        <p>$question->agecategories;
        => null</p>
      </blockquote>
    </blockquote>
  </blockquote>
  
  <p>But it has records in it and returns this after $question = App\Question::find(1);</p>
  
  <blockquote>
    <blockquote>
      <blockquote>
        <p>$question = App\Question::find(1);
        => App\Question {#2901
             que_id: 1,
             que_name: "hello",</p>
      </blockquote>
    </blockquote>
  </blockquote>
  
  <h2>Question model</h2>
  
  <pre class="lang-php prettyprint-override"><code>class Question extends Model
  {
      protected $table = 'tblquestion';
      protected $primaryKey = 'que_id';
      protected $keyType = 'integer'; 
      public $incrementing = true;
      public $timestamps = false;
  
      public function agecategories() 
      {
          return $this-&gt;belongsToMany('App\Agecategory');
      }
  }
  </code></pre>
  
  <h2>Agecategory model</h2>
  
  <pre class="lang-php prettyprint-override"><code>class Agecategory extends Model
  {
      protected $table = 'tblagecategory';
      protected $primaryKey = 'aca_id';
      protected $keyType = 'integer'; 
      public $incrementing = true;
  
      public function questions() 
      {
          return $this-&gt;belongsToMany('App\Question');
      }
  }
  </code></pre>
  
  <h2>QuestionAgecategory model</h2>
  
  <pre class="lang-php prettyprint-override"><code>class QuestionAgecategory extends Model
  {
      protected $table = 'tblquestionagecategory';
      protected $primaryKey = 'qac_id';
      protected $keyType = 'integer'; 
      public $incrementing = true;
  }
  </code></pre>
  
  <h2>Migrations</h2>
  
  <pre class="lang-php prettyprint-override"><code>      Schema::create('tblquestion', function (Blueprint $table) {
            $table-&gt;increments('que_id');
            $table-&gt;string('que_name', 128);
        });
  
  
        Schema::create('tblagecategory', function (Blueprint $table) {
            $table-&gt;increments('aca_id');
            $table-&gt;timestamps();
        });
  
        Schema::create('tblquestionagecategory', function (Blueprint $table) {
            $table-&gt;increments('qac_id');
            $table-&gt;integer('qac_que_id')-&gt;unsigned();
            $table-&gt;integer('qac_aca_id')-&gt;unsigned();
            $table-&gt;foreign('qac_que_id')-&gt;references('que_id')-&gt;on('tblquestion');
            $table-&gt;foreign('qac_aca_id')-&gt;references('aca_id')-&gt;on('tblagecategory');
        });
  </code></pre>