Laravel Eloquent: How search array of objects column of type JSON

Viewed 59

I have a database column field that contains an array of objects like this:

[
  {
    "label": "0-1",
    "key": 1
  },
  {
    "label": "1-3",
    "key": 2
  },
  {
    "label": "3-7",
    "key": 3
  }
]

How can I query by the label? To return if the matching label exists?

Update: this worked for me:

Model::whereJsonContains('column', ['label' => '0-1']);
1 Answers

You can try whereJsonContains in documentation.
https://laravel.com/docs/9.x/queries#json-where-clauses

$data = DB::table('table')
         ->whereJsonContains('column->label', '0-1')
         ->get();
Related