Laravel Json column & value is interger with where? - mysql

subcategory_id column data are in JSON format
like :
["3","4","5"]
Now I want to select all data from the table where subcategory_id = 3. How can I do this with Laravel query builder?

you can use this.
DB::table('table_name')->whereJsonContains('subcategory_id ','3')->get();

I had the same issue with whereJsonContains. Make sure you are inserting the data in a proper format, because you wouldn't get an error if you try to insert to JSON column like this:
YourTable::factory()->create([
'subcategory_id' => '3' // this should be like ['3']
]);
If the data is inserted properly, you can use whereJsonContains,
YourTable::whereJsonContains('subcategory_id', "3")->get();
Which returns all rows having "3" in subcategory_id array column.
This feature is not supported by the SQLite database

Related

How to Query Mysql text column

I have mysql 'text' column with data as json which looks like below, how to query using specific value, where Banking is model name , info is field name
Banking
info : text
info: {"age": 23, name: "John"}
In my opinion you can try search by LIKE query as below but because your database save data by text so it is very difficult to handle exception case. So I think you should try use json type of mysql or split it into table and save age as integer.
select * from bankings where info LIKE '%"age": 23%'
Ruby
Banking.where('infor LIKE ?', '%"age": 23%')
You should use ActiveRecord to perform this query using ->> operator:
Banking.where("info ->> 'name' = 'John'")

I have array of products in mysql column! How to retrieve each value in Laravel

I have saved record in Json Format in mysql column:
Example
[
{submenu_name:Hotdogs, quantity:3, price:199},
{submenu_name:CHEESE CROISSANT, quantity:1, price:12},
{submenu_name:Cakes, quantity:1, price:199}
]
Now how to retrieve these values against each item?
Laravels Query Builder provides a convenient way of querying JSON data in table columns.
Refer to the Laravel Query Builder documentation
I am missing the "" in the key and value. Its the json syntax.
[
{"submenu_name":"Hotdogs", "quantity":"3", "price":"199"},
]

Updating one row in JSON stored in MySQL

I've got a longtext field in my MySQL database that contains JSON strings. I'd like to be able to update only one row in the string rather than have to reinsert the entire thing updated.
How can I do that? I'm using Laravel but could do a raw query if needed.
(This is the first time I'm using JSON, so if I'm not using the right terminolgoy, forgive me.)
Your column needs to be of json type. then you can use the JSON_SET to set a aprticular json key in your payload.
example :
update table SETjson_column= JSON_SET(json_column, '$.\"$key\"' , 'foo') where id = 1;

How to select from a json encoded field that contains nested data?

The fields looks like this:
[{"Tester":"Bonnie","Credentials":"MS","Date":"2013-02-19"},
{"Tester":"Karen","Credentials":"Teacher","Date":"2016-01-20"}]
I need to select based on the year of the 'Date' value: < > or =
I have found the json_extract function and it's shortcuts such that this will get the data (from the MySQL docs) "autowrapped as an array" and it works:
json_field->"$[*].Date" returns ["2013-02-19", "2016-01-20"]
Great, so I has the dates from the json data, but now I need to format the WHERE. How do I select the record if either of the years is 2016 for instance? I don't see any json function that do that.
I would highly recommend you to drop this JSON column and build another table, then you can search in your table using JOIN. JSON in column is a bad idea.
Just to answer your question:
I think this should work -
WHERE YEAR(STR_TO_DATE(json_field->"$.Date", '%Y-%m-%d')) = '2016'

Mysql JSON Multi-dimensional Array

which form should have json code in mysql field ?
I need users datas (user_id is the key) with 3 values (3 informations : name , age, sex)
145:"name,age,sex",
148:"name,age,sex",
200:"name,age,sex"
I am using mysql version 5.6 and the datas will be inserted with SQL code
is it correct to store it in that way in mysql to retrieve with php and json_decode?
[{236:"paul,26,1"},{2515:"fred,42,1"},{2515:"jane,21,0"}]
thank you
Do you ever want to be able to write a query like select * from users where sex=1? If so, don't store the JSON as text. Store each value (id, name, age, sex) in a column of its own.
Even if you do want to store the JSON as a string, it would probably be better organised like this:
[
{"id":236,"name":"paul","age":26,"sex":1},
{"id":2515,"name":"fred","age":42,"sex":1},
{"id":2516:"jane","age":21,"sex":0}
]
You would need to manipulate it a bit after querying, but you would have more meaningful data.
But if all you want is to store that text, so you can retrieve it as text later, then what you have is fine.
with datas in this form
[{236:"paul,26,1"},{2515:"fred,42,1"},{2515:"jane,21,0"}]
and json_decode($myfield);
I get NULL
if I echo $myfield I get exatly what is stored in the database
what is wrong with N
[{236:"paul,26,1"},{2515:"fred,42,1"},{2515:"jane,21,0"}]
If I parse it here http://json.parser.online.fr/
I get Syntax error