I have an Eloquent Book model with a readings (JSON) field. It is an array of 12 positions, each corresponding to the number of readings of each book in each month of the current year, e.g.:
[1, 0, 3, 2, 5, 5, 2, 1, 3, 0, 0, 2]
In one of my controllers, I want to select all the books with one or more readings, by month. So I have a for loop which iterates over the months. The following is my best try, but the problem is that no books are selected with that query in any iteration, although the book from the example should be returned in all iterations except 2, 10 and 11:
for ($i = 1; $i <= 12; $i++) {
$books = Book::whereJsonDoesntContain('readings->'. $i - 1, 0)->get();
...
};
I have also tried with Book::whereRaw('not json_contains(readings->'.$i-1.', 0)'); inside the for loop, with the same luck.
Any ideas? Thanks in advance.
whereJsonContainsto query JSON arrays." Since you are looping, you can/should use normal where query,->where("readings->{$i}", '>', 0).$booksreturns empty once more. Working with JSON is differently treated as I see.