How to retrieve data based on json column matching with special data - php

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();

Related

Laravel error: Query not returning certain columns from MySQL database

I have a table with 5 columns and when I query select all columns, 1 column is ignored.
My table columns: id setting_key created_at updated_at app_setting
My query looks like this:
$result = DB::connection($token)->table('settings')->select('*')->get();
The missing column is app_setting and this exact same query is used in another part of my code and returns all 5 columns including app_setting.
If I add an addSelect to the query as so:
$query = DB::connection($token)->table('settings')->select('*');
$result = $query->addSelect('app_setting')->get();
I get an error 'Column not found: 1054' even though the column most definitely exists. Why would I be getting this error if the column exists and why does my original query not return all of the columns as it does in other areas of my code?
Edit: When I directly query the app_setting column, I get the values returned, but still get 'Column not found'. How is that possible?
first check your .env, that DB_database and DB_username and DB_password are correct or not. use the code below in the controller then run that function and post the result here
$result = DB::table('settings')->where('id',1)->first();
dd($result->app_setting);
before running the query fill a dummy data in the database.

Get data from JSON column in MySql database

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);

How to check the MySQL JSON column is not contains the value in laravel?

I used to find whether the searching item is exist in the JSON datatype column or not.
I use,
TABLE::whereRaw('json_contains(list, \'["value"]\')')->get();
this to return those are having the "value" in "list" JSON column.
But, how can i use reverse method to list, those are not having this value in json column in laravel.
Thank you !
You can use not:
TABLE::whereRaw('not json_contains(list, \'["value"]\')')->get();
In Laravel 5.6.24 you can use whereJsonDoesntContain():
TABLE::whereJsonDoesntContain('list', 'value')->get();
DB::table('table')->whereNull('list->key')->get();
list IS NULL OR JSON_CONTAINS(list, 'value') = 0

MySQL query to return json name value

I'm working with a table in which information is stored in a table in JSON format. The JSON value field looks like:
select * from k2_extra_fields where id = 2 and published = 1;
id | value
2,[{"name":"Apples","value":1,"target":null,"alias":"","required":0,"showNull":1},{"name":"Pears","value":2,"target":null,"alias":"","required":0,"showNull":1},{"name":"Mangos","value":3,"target":null,"alias":"","required":0,"showNull":1},{"name":"Guava","value":4,"target":null,"alias":"Fruit","required":0,"showNull":1},{"name":"Pineapple","value":5,"target":null,"alias":"Fruit","required":0,"showNull":1}]
Or values in a simple line by line view (minus the ID):
[
{"name":"Apples","value":1,"target":null,"alias":"","required":0,"showNull":1},
{"name":"Pears","value":2,"target":null,"alias":"","required":0,"showNull":1},
{"name":"Mangos","value":3,"target":null,"alias":"","required":0,"showNull":1},
{"name":"Guava","value":4,"target":null,"alias":"Fruit","required":0,"showNull":1},
{"name":"Pineapple","value":5,"target":null,"alias":"Fruit","required":0,"showNull":1}
]
The query that leads me here returns the value of 3. 3 = Mangos. How do I take the '3' value and match it up with the stored names/values so that I end up with the output, Mangos?
It should be possible with build in mysql functionality, but very hard and 'not clever' idea to do. If you really need to compute this problem within mysql, you would need to actually add new funtionality to your mysql. Look up on UDF plugins: http://dev.mysql.com/doc/refman/5.1/en/udf-compiling.html

Specify columns for PDO::FETCH_KEY_PAIR

I have a table with a following columns (more than 2) in my database:
1. id
2. name
3. email
I am trying to fetch key value pairs using PDO by fetching the fetchMode to PDO::FETCH_KEY_PAIR and specifying columns id and name which works just fine
But when i try to specify name and email as the desired columns i get the result set but the key is not name but usual numeric zero based indexes
0 => some#email.com
Why?
BTW i am using laravel, here is my code:
$users = new User;
$users->getConnection()->setFetchMode(PDO::FETCH_KEY_PAIR);
return $users::get(array('name','email'));
I was able to get the desired result by using:
User::lists('email', 'name');

Categories