简体   繁体   中英

My whereJsonContains not working (Laravel 5.8)

I have problem with my laravel query.

Now I use query like this:

$courses = Course::whereJsonContains('schedule->day', 1)->get();

It doesn't work.

I'm using postgreSql 9.6 and my database and raw query look like this

http://sqlfiddle.com/#!17/88fd2/1/0

I want to select class where have schedule in day = 1

If you define the column as schedule->day , MySQL assumes that this is an array of integers. In your case it's an array of objects, so you have to target the parent array and add the property name you are looking for in the second argument.

Like so:

$courses = Course::whereJsonContains('schedule', ['day' => 1])->get();

我解决了

$courses = Course::whereJsonContains('schedule', [['day' => '1']])->get();

I solved with

$products=ProductShop::active()
          ->whereJsonContains('tag', [['value' => "tampa"]])->get();

If you're querying the value уоu don't need to use whereJsonContains , simply use a regular where query such as:

$courses = Course::where('schedule->day', 1)->get();`

If you want to check if day exists in Json, use whereJsonLength such as:

$courses = Course::whereJsonLength('schedule->day', '>', 0)->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM