jeudi 27 août 2015

Query array of objects (Postgres with JSONB, Laravel 5)

I've got this data in a JSONB field:

[
    'first_name' => 'Philipp',
    'last_name' => 'Kühn',
    'size' => 170,
    'hobbies' => [
      'daily' => 'beer',
    ],
    'skills' => [
      'drink beer',
      'drink more beer',
    ],
]

I'm pretty new to Laravel and Postgres so I wanted to do some basic queries.

Here are some queries that works fine: (json is the name of the column)

$users = User::whereRaw("json ->> 'first_name' = 'Philipp'")->get();

$users = User::whereRaw("json ->> 'size' > '160'")->get();

$users = User::whereRaw("json #>> '{hobbies, daily}' = 'beer'")->get();

Now I want to check if drink beer is in skills and the following code doesn't work:

$users = User::whereRaw("json ->> 'skills' ? 'drink beer'")->get();

Here I got a syntax error:

SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "$1"
LINE 1: select * from "users" where json ->> 'skills' $1 'drink beer...
^ (SQL: select * from "users" where json ->> 'skills' ? 'drink beer')

How can I search for drink beer?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1NJpJX3
via IFTTT

Aucun commentaire:

Enregistrer un commentaire