-
app/Http/Resources/ApiResource.php
Open in GitHubuse Illuminate\Http\Resources\Json\JsonResource; class ApiResource extends JsonResource { public function toArray($request) { return parent::toArray($request); } }
-
app/Http/Controllers/Api/ProductController.php
Open in GitHubuse App\Models\Product; use Illuminate\Http\Request; use App\Http\Resources\ApiResource; use App\Http\Controllers\Controller; class ProductController extends Controller { // public function show(Request $request, Product $product) { $relatedProducts = Product::with('unit', 'category')->where('category_id', $product->categoryId) ->limit(6) ->get(); return (new ApiResource($product->load('unit', 'category'))) ->additional(['relatedProducts' => $relatedProducts]); } }