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

Posted

技术标签:

【中文标题】laravel ORM 模型在所有请求中显示相同的值【英文标题】:laravel ORM models are showing the same values in all requests 【发布时间】:2021-08-17 16:41:08 【问题描述】:

我对基本的 laravel ORM 模型有疑问。我正在调用image 模型来获取每个product 的图像文件名(product 模型具有image_id 值)所以我从数据库中获取products 并使用foreach 循环循环所有图像并添加文件名给每个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; ],以防多次使用同一模型出现问题,但没有运气,有什么建议吗?

【问题讨论】:

【参考方案1】:

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

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

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

【讨论】:

【参考方案2】:

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

您的解决方案应该是:

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

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


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

【讨论】:

以上是关于laravel ORM 模型在所有请求中显示相同的值的主要内容,如果未能解决你的问题,请参考以下文章

Laravel Eloquent ORM - 选择所有两个关系都不存在的模型时,生成的 SQL 和查询构建器结果不匹配

Laravel Eloquent ORM字段处理

Laravel Eloquent ORM 事务

laravel框架之ORM模型

ORM 在 laravel 的默认用户控制器和用户模型中不起作用

在 laravel 之外使用 ORM 找不到我的测试模型“用户”[关闭]