C
C
cadaver2020-05-13 16:59:54
JSON
cadaver, 2020-05-13 16:59:54

How to correctly execute a request with a json field in Laravel?

there is a value of the rates column in the format:

[{"rate": "1", "price": "500"}, {"rate": "2", "price": "1000"}, {"rate": "3", "price": "1500"}]


how to get rows that have price > 1000 for example?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
Ivan Ivanov, 2020-05-13
@maksim_fix

do json_decode() now you have php array.
Next, iterate over this very array using foreach, well, then you check and do what you need

I
Ilya, 2020-05-13
@New_Horizons

This is not even about laravel. You at the level of the database will not be able to filter normally like this.
As far as I know, in the latest versions of mysql (okay, not so straight in the latest ones, since 5.7), json support has appeared and even filtering by json elements. But it works terribly slowly. (this is not accurate)
If possible, change the structure of the database - do not store in json, use a separate table and a one-to-many relationship for this.
If not, then you have to do as Maxim Fix suggested .
Well, another variant, if the database is fresh, then you can try like this: JSON Where Clauses

E
Evgeny Perin, 2020-05-17
@seoperin

Try

...->whereJsonContains('rates->price', '>', '1000')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question