title: how to return a json response based on database relationship using eloquent
body: <p>I'm quite new to Laravel. I'm figuring out to work with Eloquent. </p>
  
  <p>Let's say I have 2 tables: categories and products. These two table have a one-to-many relationship. 1 category can have many products.</p>
  
  <p>I would like to return a JSON response which consists of an array of categories where each category has an array of products. It should look like this:</p>
  
  <pre><code>[
     {"name":"Category 1", "products": [
                                          {"name":"Product 1","price":"2.50"},
                                          {"name":"Product 2","price":"2.50"}
                                       ]
     },
     {"name":"Category 2", "products": [
                                          {"name":"Product 1","price":"1.95"},
                                          {"name":"Product 2","price":"2.15"}
                                       ]
     }
  ]
  </code></pre>
  
  <p><strong>Models:</strong></p>
  
  <p>Category.php</p>
  
  <pre><code>&lt;?php
  
  class Category extends Eloquent{
  
      protected $table = 'categories';
  
      public function products(){
          return $this-&gt;hasMany('Product');
      }
  }
  </code></pre>
  
  <p>Product.php</p>
  
  <pre><code>&lt;?php
  
  class Product extends Eloquent {
  
      protected $table = 'products';
  
      public function categories(){
          return $this-&gt;belongsTo('Category');
      }
  }
  </code></pre>
  
  <p><strong>Database tables:</strong></p>
  
  <p>create_categories_table:</p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Database\Schema\Blueprint;
  use Illuminate\Database\Migrations\Migration;
  
  class CreateCategoriesTable extends Migration {
  
      public function up()
      {
          Schema::create('categories', function(Blueprint $table) {
              $table-&gt;increments('id');
              $table-&gt;string('name')-&gt;unique();
              $table-&gt;timestamps();
          });
      }
  
      ...
  }
  </code></pre>
  
  <p>create_products_table:</p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Database\Schema\Blueprint;
  use Illuminate\Database\Migrations\Migration;
  
  class CreateProductsTable extends Migration {
  
      public function up()
      {
          Schema::create('products', function(Blueprint $table) {
              $table-&gt;increments('id');
              $table-&gt;string('name')-&gt;unique();
              $table-&gt;string('price');
              $table-&gt;unsignedInteger('category_id');
              $table-&gt;foreign('category_id')-&gt;references('id')-&gt;on('categories');
              $table-&gt;timestamps();
          });
      }
  
      ...
  }
  </code></pre>
  
  <p><strong>Controller:</strong></p>
  
  <p>
  
  <p>class ApiController extends BaseController {</p>
  
  <pre><code>public function getIndex(){
  
      $categories = Category::all();
  
      return Response::json(array('data' =&gt; $categories));
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I know how to return all of the categories or products or only the name of a category but I cannot seem to find the solution for the example of json response I mentioned above.</p>
  
  <p>If anyone knows this it would be really helpful. Thanks in advance.</p>