首页 > 解决方案 > 从 MySql 数据库中的 JSON 列获取数据

问题描述

在我的表中有一个包含JSON编码数据的列,一个记录看起来像这样

{"late_fee":10,
"due_date":"2019-10-01 00:00:00",
"property_type":"house"
}

从数据库获取数据时,我需要数据列late_fee中不存在键的记录。JSON我怎样才能把它放在where状态?

这是我必须添加上述条件的代码行:

$payments = Rent::where('property_id', $property['id']);`

编辑

通过您发布的评论,您能否向我确认这是正确的方法:

$payments = Rent::where('property_id', $property['id'])->whereJsonContains('notification_settings->late_fee', null);

PS是存储数据notification_settings的列的名称。JSON

标签: phplaravel

解决方案


只需阅读这篇短文: https ://laravel.com/docs/5.7/queries#json-where-clauses

那应该工作:

$payments = Rent::where('property_id', $property['id'])->whereJsonContains('notification_settings->late_fee', null);

推荐阅读