title: Laravel - Many to Many relationship with Datatables.js and Yajra Package
body: <p>I need some help, please.</p>
  
  <p>I have two models, <code>Country</code> and <code>Buy</code>, with <code>Many to many relationship</code> between them. I'm using the <a href="https://github.com/yajra/laravel-datatables" rel="nofollow noreferrer">Laravel Datatables</a> by <strong>Yajra</strong> package to display a table of all the countries and buys.</p>
  
  <p><strong>Countries</strong></p>
  
  <pre><code>Schema::create('countries', function (Blueprint $table) {
      $table-&gt;increments('id');
      $table-&gt;string('name');
      $table-&gt;timestamps();
  });
  </code></pre>
  
  <p><strong>Buys</strong></p>
  
  <pre><code>Schema::create('buys', function (Blueprint $table) {
      $table-&gt;increments('id');
      $table-&gt;string('name');
      $table-&gt;integer('code_one');
      $table-&gt;integer('code_two');
      $table-&gt;timestamps();
  });
  </code></pre>
  
  <p><strong>buy_country</strong></p>
  
  <pre><code>Schema::create('buy_country', function (Blueprint $table) {
      $table-&gt;increments('id');
      $table-&gt;integer('buy_id');
      $table-&gt;integer('country_id');
      $table-&gt;timestamps();
  });
  </code></pre>
  
  <p><strong>Models</strong></p>
  
  <pre><code>class Country extends Model
  {
      public function buys(){
          return $this-&gt;belongsToMany('App\Buy', 'buy_country');
      }
  }
  
  class Buy extends Model
  {
      public function countries(){
          return $this-&gt;belongsToMany('App\Country', 'buy_country');
      }
  }
  </code></pre>
  
  <hr>
  
  <p>I want to display the country name on the first row, and the products from buys table on child rows.</p>
  
  <p><a href="https://i.stack.imgur.com/tvnma.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tvnma.png" alt="enter image description here"></a></p>
  
  <p>How can I do this, with or without Yajra package?</p>