Laravel eloquent 过滤 json 列的值

sab*_*nan 3 laravel eloquent laravel-8

我接手了一个项目,他们将date_start值存储在 JSON 列中。

(例如,id、name、some_column、meta)
其中meta的值为{ propert1, property2, property3, "date_start": "2021-09-26", and so on...}

现在,在前端,我必须创建一个搜索功能,在其中选择一个日期。

现在在后端,由于 的值date_start已存储在 json 列中,我假设我需要首先获取所有记录,循环遍历它们,然后解码该meta字段。

总体来说不是很糟糕吗?或者还有其他办法吗?

Maa*_*Dev 10

现代版本的数据库通常支持 JSON 列类型。可以使用 SQL 查询这些 json 列。

preferences表中的列users

{"dining": {"meal": "salad", "time": "20:00"}}
Run Code Online (Sandbox Code Playgroud)

询问:

$users = DB::table('users')
                ->where('preferences->dining->meal', 'salad')
                ->get();
Run Code Online (Sandbox Code Playgroud)

查看 Laravel 文档中关于JSONWhere 子句的内容