如何在Laravel中使用where子句编写查询?

时间:2019-12-28 12:57:26

标签: laravel laravel-5

存储在数据库表字段“ contact_info”中的json记录下方,数据类型为文本

{
  "invoice":{
    "number":"gthy",
    "invoiceDate":"2019-12-25 17:20:08",
    "invoiceDueDate":"2020-01-01 17:20:08",
    "paymentTerms":"7"
  },
  "company":{
    "name":"Test ",
    "address":"Test",
    "city":"Test",
    "state":"Test",
    "country":"Test",
    "pincode":"Test",
    "email":"Test@gmail.com",
    "currency_code":"USD"
  },
  "sender":{
    "name":"Test",
    "address":"Test",
    "city":"Test",
    "state":"Test",
    "country":"Test",
    "pincode":"Test",
    "email":"Test"
  }
}

我需要在Laravel中的“ invoiceDueDate”此字段中应用条件。那我该如何写查询呢?请建议我。

1 个答案:

答案 0 :(得分:0)

可以使用以下查询:

$contacts = Contact::where('contact_info->invoice->invoiceDueDate', '2020-01-01 17:20:08')->get()

要在特定日期后获取所有带有invoiceDueDate的记录,

$contacts= Contact::where('test->invoice->invoiceDueDate', '>=','2020-01-01')->get()

不需要JSON支持的替代方法:

Contact::where('contact_info', 'like','%"invoiceDueDate":"2020-01-01 17:20:08"%')->get()

签出laravel的json querying documentation