Having issues looping through variants for Commerce 2 Products using Element API
You are setting the whole $variants variable to last item in the array instead of creating the $variants array variable and pushing items into it. Should be this:

'transformer' =&gt; function (Product $product) {
          $variants = [];

          foreach ( $product-&gt;getVariants() as $variant ) {
            $variants[] = [
              'type'          =&gt; $product-&gt;type-&gt;name,
              'title'         =&gt; $variant-&gt;title,
              'sku'           =&gt; $variant-&gt;sku,
              'price'         =&gt; $variant-&gt;price,
              'stock'         =&gt; $variant-&gt;stock,
              'intro'         =&gt; $variant-&gt;intro,
              'purchasableId' =&gt; (int) $variant-&gt;purchasableId,
              'status'        =&gt; $variant-&gt;status,
              'url'           =&gt; $variant-&gt;url,
              // 'productPhoto'  =&gt; $variant-&gt;productPhoto
            ];
          }

          return $variants;
        }