title: laravel api resource add other data
body: <p>Supposed I have an API response of a Product </p>
  
  <pre><code>{
      data: [
      {
          id: 3,
          name: "Test Product",
          price: "158.21",
          quantity: 4,
          income: 569.56
      },
      {
          id: 4,
          name: "Test Product",
          price: "58.21",
          quantity: 3,
          income: 157.17
      },
      ]
  }
  </code></pre>
  
  <p>is there a way where i can add a total of all the income of a product just like this?</p>
  
  <pre><code>{
   data: [
      {
          id: 3,
          name: "Test Product",
          price: "158.21",
          quantity: 4,
          income: 569.56
      },
      {
          id: 4,
          name: "Test Product",
          price: "58.21",
          quantity: 3,
          income: 157.17
      },
      ],
      total: 726.73
  }
  </code></pre>
  
  <p>this is my class <code>OrderProductResource</code> that extends <code>JsonResource</code></p>
  
  <pre><code>public function toArray($request)
      {
          $quantity = 0;
          $overAllTotal = 0;
          foreach($this-&gt;orders as $order){
              $quantity += $order-&gt;pivot-&gt;quantity; 
          }
          $sub_total = round($this-&gt;price * $quantity,2);
          $discount = round((10 / 100) * $sub_total, 2);
          $totalIncome = round(($sub_total - $discount), 2);
          return [
              'id' =&gt; $this-&gt;id,
              'name' =&gt; $this-&gt;name,
              'price' =&gt; $this-&gt;price,
              'quantity' =&gt; $quantity,
              'income' =&gt; $totalIncome,
          ];
      }
  </code></pre>
  
  <p>i tried to use the <code>with</code> method in laravel but the API response is still the same. </p>
  
  <p>this is my controller</p>
  
  <pre><code>public function index(){
          $errorFound = false;
          $error = ['error' =&gt; 'No Results Found'];
          $products = Product::with('orders');
          if (request()-&gt;has('q')) {
              $keyword = '%'.request()-&gt;get('q').'%';
              $builder = $products-&gt;where('name', 'like', $keyword);
              $builder-&gt;count() ? $products = $builder : $errorFound = true;
          }
          return $errorFound === false ? OrderProductResourceCollection::collection($products-&gt;latest()-&gt;paginate()) : $error;
      }
  </code></pre>