title: how to format responses JSON with Relationship - Laravel/Eloquent
body: <p>I'm working on a Laravel project and I want to create a REST API for a website. On my system, I have two tables: my tables are Item and Product table which both have one to one relationship i want the json response from two tables like below</p>
  <pre><code>&quot;data&quot;: [
          {
              &quot;product_id&quot;: 3,
              &quot;product_name&quot;: &quot;xyz&quot;,
              &quot;sold&quot;: 0,
              &quot;total&quot;: 500
              }
          }
          ]
  </code></pre>
  <p>but the actual format am getting is like below</p>
  <pre><code>&quot;data&quot;: [
          {
              &quot;product_id&quot;: 3,
              &quot;product_name&quot;: &quot;xyz&quot;,
              &quot;prod&quot;: {
                  &quot;id&quot;: 1,
                  &quot;products_id&quot;: 3,
                  &quot;sold&quot;: 0,
                  &quot;total&quot;: 500
              }
          }
          ]
  </code></pre>
  <p>My Item Controller class</p>
  <pre><code>class ItemCont extends BaseController
  {
      
      public function index()
      {
          $items= Items::all();
  
          return $this-&gt;sendResponse(ItemResource::collection($items), 'Items retrieved successfully.');
      }
  }
  </code></pre>
  <p>My ItemResource class</p>
  <pre><code>     class ItemResource extends JsonResource
  {
     
      public function toArray($request)
      {
          
          return parent::toArray($request);
  
         
      }
  }
  </code></pre>
  <p>Items Model</p>
  <pre><code>    class Items extends Model
      {
          public $timestamps = false;
          protected $primaryKey = 'product_id';
      
          protected $guarded = [];
          protected $fillable = [
              'product_name'
          ];
      
             
          public function prod(){
              return $this-&gt;hasOne('App\Products','products_id','product_id');
          }
      }
  products Model
      class Products extends Model
      {
          public $timestamps = false;
          protected $primaryKey = 'id';
      
          protected $guarded = [];
          protected $fillable = [
              'sold','total'
          ];
      
          
          public function item(){
              return $this-&gt;belongsTo('App\Items','product_id','products_id');
          }
       
          }
  products resource class
  class ProductResource extends JsonResource
  {
      /**
       * Transform the resource into an array.
       *
       * @param  \Illuminate\Http\Request  $request
       * @return array
       */
      public function toArray($request)
      {
               
         return parent::toArray($request);
        
      }
  </code></pre>
  <p>Thanks
  The second method i Have tried is  my ItemResource class</p>
  <pre><code>class ItemResource extends JsonResource
  {
      /**
       * Transform the resource into an array.
       *
       * @param  \Illuminate\Http\Request  $request
       * @return array
       */
      public function toArray($request)
      {
          
          //return parent::toArray($request);
  
          return [
              'Product_id' =&gt; $this-&gt;id,
              'product_name' =&gt; $this-&gt;name
              //'products' =&gt; new Products($this-&gt;products),
              //'sold' =&gt; $this-&gt;sales,
              //'total' =&gt; $this-&gt;total,
          ];
      } 
  </code></pre>
  <p>my Product Resource class</p>
  <pre><code>class ProductResource extends JsonResource
  {
      /**
       * Transform the resource into an array.
       *
       * @param  \Illuminate\Http\Request  $request
       * @return array
       */
      public function toArray($request)
      {
                 return [
              &quot;sold&quot; =&gt; $this-&gt;sales,
           &quot;total&quot;=&gt;$this-&gt;total
              
         ];
           //   return parent::toArray($request);
  
        
      }
  </code></pre>
  <p>and
  my ItemController class</p>
  <pre><code>class ItemCont extends BaseController
  {
      /**
       * Display a listing of the resource.
       *
       * @return \Illuminate\Http\Response
       */
      public function index()
      {
          $items= Items::with('prod')-&gt;get();
  
          return $this-&gt;sendResponse(ItemResource::collection($items), 'Items retrieved successfully.');
      }
  </code></pre>
  <p>Response for second method try is</p>
  <pre><code>{
      &quot;success&quot;: true,
      &quot;data&quot;: [
          {
              &quot;Product_id&quot;: null,
              &quot;product_name&quot;: null
          },
          {
              &quot;Product_id&quot;: null,
              &quot;product_name&quot;: null
          }
      ],
      &quot;message&quot;: &quot;Items retrieved successfully.&quot;
  }
  </code></pre>