我在产品表中有一列名为 primary_colors,它以以下格式存储数据
["5","7","11"]
当用户过滤产品并选择所有颜色假设
primary_color => array:1 [▼
0 => "5",
1 => "11"
]
如何使用 laravel 进行搜索?
要在
products
表中搜索 JSON 中的 primary_colors
列,您可以使用 whereJsonContains
。例如:
$primaryColors = [5, 11];
$products = DB::table('products')
->whereJsonContains('primary_colors', $primaryColors)
->get();
您只需要注意 DB 上的 JSON 列类型支持:MySQL 5.7+、PostgreSQL、SQL Server 2016 和 SQLite 3.39.0.