Laravel whereJsonContains查询不工作
我正试图回答这个问题:Laravel whereJsonContains查询不工作,laravel,eloquent,laravel-8,Laravel,Eloquent,Laravel 8,我正试图回答这个问题: $order = Order::whereJsonContains('products->id',$serial_no)->first(); 但它不起作用,请帮我解决这个问题。提前谢谢 [ { "id": 24, "name": "test product", "qnt": 3, "sale_price": 18,
$order = Order::whereJsonContains('products->id',$serial_no)->first();
但它不起作用,请帮我解决这个问题。提前谢谢
[
{
"id": 24,
"name": "test product",
"qnt": 3,
"sale_price": 18,
"buy_price": 14,
"stock": 3,
"total": 54,
"company": {
"id": 1,
"name": "test company",
"contact": "01521482467,5555555",
"address": "fdsa fdsa fdsa fdsa fdsa",
"is_active": 1
},
"brand": {
"id": 1,
"name": "Test Brand",
"is_active": 1
},
"category": {
"id": 1,
"name": "Test",
"is_active": 1
}
},
{
"id": 28,
"name": "test2",
"qnt": 1,
"sale_price": 18,
"buy_price": 12,
"stock": 1,
"total": 18,
"company": {
"id": 1,
"name": "test company",
"contact": "01521482467,5555555",
"address": "fdsa fdsa fdsa fdsa fdsa",
"is_active": 1
},
"brand": {
"id": 1,
"name": "Test Brand",
"is_active": 1
},
"category": {
"id": 2,
"name": "Test2",
"is_active": 1
}
}
]
使用get()而不是first()再试一次。使用get()而不是first()再试一次。@md abdul karim请检查您的mysql版本,以及mysql json_包含的与mysql 5.7或更高版本一起工作的内容。@md abdul karim请检查您的mysql版本,好的,mysql json_包含了mysql 5.7或更高版本的工作。试试这些 当您的json是
{
"id": 24,
"name": "test product",
"qnt": 3,
"sale_price": 18,
"buy_price": 14,
"stock": 3,
"total": 54,
"company": {
"id": 1,
"name": "test company",
"contact": "01521482467,5555555",
"address": "fdsa fdsa fdsa fdsa fdsa",
"is_active": 1
}
那么你的代码就可以正常工作了。
但是您的JSON
在数组中
[
{
"id": 24,
"name": "test product",
"qnt": 3,
"sale_price": 18,
"buy_price": 14,
"stock": 3,
"total": 54,
"company": {
"id": 1,
"name": "test company",
"contact": "01521482467,5555555",
"address": "fdsa fdsa fdsa fdsa fdsa",
"is_active": 1
}
]
因此,您可以使用这些语法解决您的问题
$order = Order::whereJsonContains('field_name', ['object_key' => 'object_value'])->first();
试试这些
当您的json是
{
"id": 24,
"name": "test product",
"qnt": 3,
"sale_price": 18,
"buy_price": 14,
"stock": 3,
"total": 54,
"company": {
"id": 1,
"name": "test company",
"contact": "01521482467,5555555",
"address": "fdsa fdsa fdsa fdsa fdsa",
"is_active": 1
}
那么你的代码就可以正常工作了。
但是您的JSON
在数组中
[
{
"id": 24,
"name": "test product",
"qnt": 3,
"sale_price": 18,
"buy_price": 14,
"stock": 3,
"total": 54,
"company": {
"id": 1,
"name": "test company",
"contact": "01521482467,5555555",
"address": "fdsa fdsa fdsa fdsa fdsa",
"is_active": 1
}
]
因此,您可以使用这些语法解决您的问题
$order = Order::whereJsonContains('field_name', ['object_key' => 'object_value'])->first();
尝试但不工作:(尝试但不工作:(mysql版本15.1发行版10.3.25-MariaDB,用于debian linux gnu(x86_64)我不知道我的系统如何显示这一点:(mysql版本15.1发行版10.3.25-MariaDB,用于debian linux gnu(x86_64)我不知道我的系统如何显示这一点:(