laravel ORM 模型在所有请求中显示相同的值

laravel ORM models are showing the same values in all requests

我对基本 laravel ORM 模型有疑问。我正在调用 image 模型来获取每个 product 的图像文件名(product 模型具有 image_id 值)所以我从数据库中获取 products 并使用 foreach loop 循环所有图像并为每个图像添加文件名 product:

foreach($products as $product) {
    $pimg = image::find($product->image_id)->first()->filename;
    $product->imagefilename = $pimg;
}

问题是结果中的所有产品在 imagefilename(JSON 编码响应)中显示相同的文件名:

[
    {
        "id": 1,
        "name": "dell-v3557",
        "description": "this is dell v3557 ,this is description",
        "short_description": "short description of dell v3557",
        "category_id": 1,
        "subcategory_id": 1,
        "image_id": 1,
        "store_id": 1,
        "price": 2100,
        "discount_price": 1800,
        "count": 5,
        "countries": "all",
        "created_at": "2021-05-28T11:07:10.000000Z",
        "updated_at": "2021-05-28T11:07:10.000000Z",
        "imagefilename": "dell-v3557.png"
    },
    {
        "id": 2,
        "name": "gr5-2017",
        "description": "this is gr5 2017 , lorem ipsum dolor , this is description",
        "short_description": "short description of gr5",
        "category_id": 1,
        "subcategory_id": 1,
        "image_id": 2,
        "store_id": 1,
        "price": 700,
        "discount_price": 550,
        "count": 2,
        "countries": "all",
        "created_at": "2021-05-28T11:07:10.000000Z",
        "updated_at": "2021-05-28T11:07:10.000000Z",
        "imagefilename": "dell-v3557.png"
    },
    {
        "id": 3,
        "name": "iphone 11 pro",
        "description": "this is iphone 11 pro , lorem ipsum dolor , this is description",
        "short_description": "short description of iphone 11 pro",
        "category_id": 1,
        "subcategory_id": 1,
        "image_id": 4,
        "store_id": 1,
        "price": 1400,
        "discount_price": null,
        "count": 8,
        "countries": "all",
        "created_at": "2021-05-28T11:07:10.000000Z",
        "updated_at": "2021-05-28T11:07:10.000000Z",
        "imagefilename": "dell-v3557.png"
    },
    {
        "id": 4,
        "name": "macbook pro",
        "description": "this is macbook pro , lorem ipsum dolor , this is description",
        "short_description": "short description of macbook pro",
        "category_id": 1,
        "subcategory_id": 1,
        "image_id": 5,
        "store_id": 1,
        "price": 1850,
        "discount_price": 1700,
        "count": 13,
        "countries": "all",
        "created_at": "2021-05-28T11:07:10.000000Z",
        "updated_at": "2021-05-28T11:07:10.000000Z",
        "imagefilename": "dell-v3557.png"
    }
]

我也尝试在每个循环中使用 [ $img = new image; ] 以防多次使用相同模型时出现问题,但运气不好,有什么建议吗?

我设法修复了它,问题出在 ::find() 方法中。我用 ::where 代替:

$pimg = image::where("id",$product->image_id)->first()->filename;

现在它可以正确获取图像的文件名而不会重复。

你的解决方案是正确的,但问题不在于find,而是你已经完成了find(...)->first(),所以它得到了图像,但你随后做了first所以你得到又是第一个 Images 模型...

您的解决方案应该是:

$pimg = image::find($product->image_id)->filename;

这是Laravel方式,不是你的方式。


find($id)where(PRIMARY_MODEL_KEY_COLUMN, $id)->first()...

别名