如何在Laravel中更新json类型列对象的数据?

olqngx59  于 2023-01-31  发布在  其他
关注(0)|答案(1)|浏览(122)

我有一个json类型的对象列在我的表(属性)在MySQL中像:

[
    {
        "unit": "2",
        "floor": "1",
        "price": "6000000",
        "toilet": "2",
        "balcony": "2",
        "bedrooms": "2",
        "customer": "3",
        "bathrooms": "3",
        "flat_name": "1A",
        "flat_size": "1200",
        "floor_plan": "217",
        "price_per_sqft": "5000"
    },
    {
        "unit": "2",
        "floor": "1",
        "price": "5000000",
        "toilet": "2",
        "balcony": "2",
        "bedrooms": "2",
        "customer": null,
        "bathrooms": "3",
        "flat_name": "1B",
        "flat_size": "1200",
        "floor_plan": "215",
        "price_per_sqft": "5000"
    },
    {
        "unit": "1",
        "floor": "2",
        "price": "6000000",
        "toilet": "2",
        "balcony": "2",
        "bedrooms": "2",
        "customer": null,
        "bathrooms": "3",
        "flat_name": "2A",
        "flat_size": "1250",
        "floor_plan": "216",
        "price_per_sqft": "5300"
    }
]

如何更新客户ID,其中flat_name = 1B来自Laravel中的此对象?

pbpqsu0x

pbpqsu0x1#

从数据库提取数据后使用集合:

$target= $collection->filter(function ($item) {
    return $item->flat_name=="1B";
})->values();

或在获取数据之前使用雄辩的过滤器:

Model::where('flat_name','1B')->get();

相关问题