2

I have an array of JSON objects within my database (as depicted by the image below)

enter image description here

How do I search for a specific value within it? Say I want the record where the "ends_at" field is equal to "2018-11-24 08:00:00"

I've tried using

->where('column->starts_at', '2018-11-24 08:00:00')

or even tried

->whereJsonContains('column->starts_at', '2018-11-24 08:00:00').

Within the model I've cast the column to an array - am I missing something, or can this not be done?

1
  • May be you can try this : DB::table('your_table')->whereRaw('json_contains(column, \'["value"]\')')->get(); Commented Oct 26, 2018 at 12:08

1 Answer 1

10

Use this:

->whereJsonContains('column', ['starts_at' => '2018-11-24 08:00:00'])
Sign up to request clarification or add additional context in comments.

6 Comments

@HelenChe Do you mean ->where('column->starts_at', '2018-11-24 08:00:00')?
Sorry I meant <, > and such. I took a quick look at the Builder code but I'm not seeing any options for this...
Do you mean ->where('foo->bar', '<', 123)?
Yes... but that is only if foo->bar is simply key-value from my understanding. Apologies if I'm not making sense but let's try the original question posted as example: how would I get results of the JSON array where availability > 9?
I've tried ->whereJsonContains('column', ['availability', '>', 9]) will not work unfortunately.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.