title: Join two table in laravel
body: <p>I have two tables like below.</p>
  
  <pre><code>Product TABLE
  
  id name count
  1  book 3
  2  shoes 4
  3  pen 3
  </code></pre>
  
  <pre><code>Category TABLE
  
  id category_id product_id
  1    4           1
  2    5           1
  3    1           2
  4    3           3
  </code></pre>
  
  <p>I want to get the result, when i query like <code>... where product.id = 1</code>.</p>
  
  <pre><code>{
    "id" : 1,
    "name" : "book",
    "count" : 3,
    "categories" : [
        4,
        5
      ],
  }
  </code></pre>
  
  <p>The following codes are showing models and their relationship.</p>
  
  <p>Product in model</p>
  
  <pre><code>class Product extends Model
  {
      protected $fillable = [
          'name',
          'count',
      ];
  
      public function categories()
      {
          return $this-&gt;hasMany(Category::class);
      }
  }
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code> ...
   $product = Product::with('categories')-&gt;get();
   ...
  
  </code></pre>
  
  <p>But result is like this.</p>
  
  <pre><code>{
    "id" : 1,
    "name" : "book",
    "count" : 3,
    "categories" : [
        {
           "category_id": 4
        },
        {
           "category_id": 5
        }
      ],
  }
  
  </code></pre>
  
  <p>I think i should use the relationship between two tables.</p>