Laravel query json column array - php

I am trying to access specific data in a json column using a laravel controller, the DB column is called 'figuresinorder'. I want to access the "wants" key but it's not working when there are multiple values stored.
{"wants": ["1"], "trades": ["12,33,234"]} - this works
{"wants": ["1,2,3"], "trades": ["12,33,234"]} - does not work
The query in the controller is as follows:
$figures2 = customtrades::whereJsonContains('figuresinorder->wants', ['1'])->get();
Any help will be greatly received, been stuck on this for longer than I dare to admit.

MySQL & PostgreSQL support whereJsonContains() with multiple values like this way :
customtrades::whereJsonContains('figuresinorder->wants', ['1','2','3'])->get();
For more, see the documentation here

Related

Laravel 9 how to best query JSON object in JSON column?

I've got a basic app up and running in the latest version of Laravel 9 that's utilising JSON columns for storing certain bits of data. I have a job_type_rates column on my Client model/table, where some have a value similar to:
[
{
"job_type": "8",
"pay_rate": "15.45",
"charge_rate": "18.45",
"awr_pay_rate": "21.33",
"awr_charge_rate": "26.77"
}
]
What I would like to do is select all clients that have a job_type of 8. I've tried to do Client::whereJsonContains('job_type_rates->job_type', "8")->get() but no results are returned, however that code would work if I didn't have an object in the column.
One way I can get around this is to create a pivot table and go down that route, but I was wondering if anyone had come up against this before and perhaps used a closure or similar?
Based on the comment by #RiggsFolly I tried this code:
Client::whereJsonContains('job_type_rates', ["job_type" => "8"])->get()
And it works, it returns the expected results. As far as I'm aware this isn't in the Laravel docs (which mostly show single value examples).
I think it's still better to extract this out into a pivot table or similar, but I hope this helps someone!

Can't get result from where condition with variables in laravel

this is how i'm trying to get the type of certificate with where condition , but still recieve
nothing from this query:
$res= student::find($student, ['typecertificate']);
$k = certificate::select('id-cer')->where('name-cer','=',$res)->get();
return $k;
Based on your comments, I'm assuming you want to retrieve the field certificateType from the latest record that was inserted in the students table.
You can achieve that without a where clause, by directly using the Eloquent Builder to retrieve only that specific field like this:
Student::latest()->first('certificateType');
But this would give you an Eloquent Collection with one element. If you just want the value (not wrapped in a collection), you can simply retrieve the latest student and get the corresponding field directly:
$certificateType = Student::latest()->first()->certificateType;
I could explain more, but your question is vague and your database schema isn't clear either, so I'd need more information on that as well as what you intend to achieve.
In any case, Laravel's documentation is often a big help: https://laravel.com/docs/9.x/eloquent#retrieving-single-models

SELECT for an array field -- Magento

One of the fields in my seller db has following data:
{"delivery area":"delivery amount"}
An example of this data in the db is as follows:
{"1":"0","2":"0","3":"0","4":"200","5":"1"}
Now I want to write a query that picks all sellers that deliver to a particular delivery area.
How can I do it using SQL? What do I specify in the WHERE clause?
I am working on Magento so an equivalent in addFieldToFilter will also help.
I just realized that this value is nothing but a json encoded value. All I had to do was to decode it upon reading and extract the values.

HBase - How to query based on key-timestamp-id?

All-
New to HBase and I've finally been able to actually take data I was once storing in MySQL (about 50 million rows) and insert it into my HBase table.
I'm now trying to query this data based on the keys and am running into some problems.
Basically I have a key that is constructed like:
objectname-createdtime-customerid
Now I need to query based on the objectname and a range for the createdtime, does anyone know how I can do this? (I'm using PHP/Thrift, but I don't need it to be a specific answer to this)
I can query if I know the exact row/key, I just need to know how to specify a range now for the middle property.
Thanks in advance!
Use a scan where the start row is the one with key objectname-<min created time>-customerid and the stop row has key objectname-<max created time>-customerid.
http://wiki.apache.org/hadoop/Hbase/ThriftApi#Scanner_methods

flex php mysql - post multiple rows of data

There are a few examples on the web showing how to post a row/form to mysql with flex php. I think it will be valuable if it allows user to post multiple rows of data. Supposed I have a datagrid and its id is myDG, how can I post the datagrid data to a table in mysql? I am using Flash Builder 4 and I need a solution based on Flex 3/4 + PHP + MySQL. Any suggestions?
I think this should be done this way:
Create a ValueObject class with several params, some used in the data grid and some not.
Insert dynamically using DataGrid.dataProvider.addItem(myValueObject);
Create a PHP function with 2 params, $number_of_rows (in flex is DataGrid.dataProviderLength), and the other param is $item, with is the DataGrid.dataProvider.
The INSERT function might be something like this:
INSERT INTO table (producto, id, precio, stock)
VALUES ("$producto", "$id", "$precio", "$stock")
, ("$producto2", "$id2", "$precio2", "$stock2")
//this must be repeated the number of rows
, ("$productoN", "$idN", "$precioN", "$stockN");
//Several times
Sometimes after placing the call in flex the data is not inserted, so you need something like this myphpService.commit();
I will try to make a full example later.
Thank you.

Categories