当我 return 一个产品及其图像处于变形关系时,它 return 是所有产品

When i return one product with its images in morph relation it returns all the products

我在通过 id

获取一种产品时遇到问题
Route::get('product/{product}', 'Api\ProductController@show');

我的 show 方法如下所示:

    public function show(Product $product)
    {
        return $product->with('images')->get();
    }

it should return back to me only one product instead it returns all the products

Product 模型中的变形关系

    public function images()
    {
        return $this->morphMany(Image::class, 'imageable');
    }

实际上,最大的问题是 get() 的使用,returns 以这种方式收集了所有产品。试试这个:

public function show(Product $product)
{
   $product->load('images');

   return $product;
}