In my table there is a column with JSON encoded data and one record looks something like this
{"late_fee":10,
"due_date":"2019-10-01 00:00:00",
"property_type":"house"
}
When getting data from database I need records where key late_fee does not exist in JSON data column. How can I put it in where condition?
This is the line of code on which I have to add mentioned condition:
$payments = Rent::where('property_id', $property['id']);`
EDIT
By comments you posted, can you confirm me that this is the right way:
$payments = Rent::where('property_id', $property['id'])->whereJsonContains('notification_settings->late_fee', null);
P.S. notification_settings is the name of column where JSON data is stored.
Just read this short article:
https://laravel.com/docs/5.7/queries#json-where-clauses
That should work :
$payments = Rent::where('property_id', $property['id'])->whereJsonContains('notification_settings->late_fee', null);
Related
delivery_charges table has a json type column named locations. this column has the following data
[{"district_id":"1"},{"district_id":"2"},{"district_id":"3"}]
But $deliveryCharges is always an empty array when I run this code:
$deliveryCharges = DB::table('delivery_charges')
->whereJsonContains('locations->district_id',post('district_id'))
->get();
dump(post('district_id')); // outputs 1
dump($deliveryCharges); // outputs []
I can see value of post('district_id') is 1. And when I comment whereJsonContains() lines it return the data. So it seems to me there is a problem in my where clause.
If your column is named location, in the where clause you are named locations. Just use the correct column name:
$query->whereJsonContains('location->district_id',post('district_id'));
Try this
$deliveryCharges = DB::table('delivery_charges')
->where('locations','LIKE','%"district_id":"'.post('district_id') .'"%')
->get();
I have a mysql table with a JSON column with data looking like that :
"[
{\"messageId\":\"0A0000000123ABCD1\",\"status\":\"request\\/0\",\"messagePrice\":\"0.03330000\"},
{\"messageId\":\"0A0000000123ABCD2\",\"status\":\"request\\/0\",\"messagePrice\":\"0.03330000\"}
]"
How can I query the database to get the row containing a specific messageId (messageIds are uniques) ?
I tried :
DB::table('my_table')
->whereJsonContains('my_json_column',[['messageId'=>'0A0000000123ABCD1']]);
But I get no results.
Edit :
I can make it work with a simple where like :
DB::table('notification_history')
->where('nexmo_responses','like','%0A0000000123ABCD1%')
->get();
But it's not really a clean way to do it.
Your data looks like it's been double-encoded when it was inserted. That's the root of your issue.
If you ensure the data is only encoded once, then it'll be readable correctly when you want to access it again.
i want to update my column data in a way that posted data gets appended at the end of the existing column data. Current data is in Json format
This is how i am updating record
$data=array('services', $array );
$this->db->where('id',$id)
$this->db->update('garage',$data);
but this updates whole record how do i append the record at the end of json
You can select the data from the database and use PHP way to concatenate the string and run the update.
$d=$this->db->get_where('garage',array('id',$id))->row();
Now merge the you can merge the existing data with new data.
$new_data=$d->services.json_encode($array);
$data=array('services', $new_data );
$this->db->where('id',$id)
$this->db->update('garage',$data);
Hope this will fix the issue.
I have JSON data that is stored in a variable which is a set of permissions for a particular user. The JSON data however, is not stored in a file, it's just in a database column.
I've tried to look for a method of updating a permission (which is either true or false) but I've had no luck thus far. At the moment, I have something to the effect of;
The raw JSON data...
{
"permissions": {
"permission_1": true,
"permission_2": false
}
}
Getting it out of the database...
$permissions = json_decode($data, true);
How do I (using PHP) update the JSON data to say, update permission 2 to true? I'm using json_decode() when it comes out of the database and putting it into an array but that's only for using it. After that I'm lost as to how to update the value.
In order:
Extract column data from the database.
"SELECT column FROM table WHERE id = 1 LIMIT 1"
JSON_decode data into a set of PHP values/objects/arrays/whatever.
$array = json_decode($OutputRow['column'],true);
Update the value(s) you need to update.
$array['permissions']['permission2'] = true;
Recompile into a JSON string using JSON_encode.
$data = json_encode($array);
Update the database (SQL?) with the new JSON_encoded string.
"UPDATE table SET column = :data WHERE id = 1 LIMIT 1"
My examples use PDO and MySQL by reference but does not go into any details about database interaction as you've not given any details as to how you're reaching your database. It's only a ballpark rough example of how to do the points listed.
I have a database where I store some data, and one field is a json string.
I insert an json in this field:
$stmt->bindParam(":settings", json_encode($widget->settings));
Then when I try to retrieve the record I get my row with the settings column as string. I need my data to be json, so I should decode this field before output my records. If i go with:
$app->response->setBody(json_encode($data, JSON_NUMERIC_CHECK));
I get something like:
"name":"My Name","label":null,"row":null,"settings":"{\"site\":\"dfsdf\",\"action\":\"UrlInfo\"}"
with settings escaped. I should first decode settings and then encode again to output my results. How can I do to solve this?
UPDATE:
I retrieve my data using PDO, so I get an array:
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
Before I save this I have:
"settings":{"site":"fff","action":"UrlInfo"}}
When you retrieve the data, you should use json_decode to reverse the encoding that you did when you inserted it.
foreach ($data as &$row) { // Use reference so we can modify in place
$row['settings'] = json_decode($row['settings']);
}
$app->response->setBody(json_encode($data, JSON_NUMERIC_CHECK));