Laravel Eloquent - Working with an array of JSON objects - arrays

I have an array of JSON objects within my database (as depicted by the image below)
How do I search for a specific value within it? Say I want the record where the "ends_at" field is equal to "2018-11-24 08:00:00"
I've tried using
->where('column->starts_at', '2018-11-24 08:00:00')
or even tried
->whereJsonContains('column->starts_at', '2018-11-24 08:00:00').
Within the model I've cast the column to an array - am I missing something, or can this not be done?

Use this:
->whereJsonContains('column', ['starts_at' => '2018-11-24 08:00:00'])

Related

FireStore and maps/arrays, document-list to array in Kotlin

I've finally started to understand a lot of info regarding FireStore, but I'm wondering if I can get some assistance.
If I had a setup similar to or like this:
          races
                Android
                      name: Android
                      size: medium
                       stats          <---- this is the map
                                str: 10
                                sex: 12.... (more values)
How would I parse this? I am looking to make specific TextViews apply values found in the database so that I can simply update the database and my app will populate those values so that hard coding and code updating won't be nearly as troublesome in the future.
I currently use something like this:
val androidRef = db.collection("races").document("Android")
androidRef.get().addOnSuccessListener { document ->
if (document != null) {
oneOfTheTextViews.text = document.getString("str")
} else {
}
The issue is currently I can only seem to access from collection (races) / document (android) / then a single field (I have "str" set as a single field, not part of a map or array)
What would the best practice be to do this? Should I not nest them at all? And if I can reference said nesting/mapping/array, what functions need to be called? (To be clear, I am not asking only whether or not it is possible - the reference guides and documents allude to such - but what property/class/method/etc needs to be called in order to access only one of those values or point to one of those values?).
Second question: Is there a way to get a list of document names? If I have several races, and simply want to make a spinner or recycler view based on document names as part of a collection, can I read that to the app?
What would the best practice be to do this?
If you want to get the value of your str property which is nested within your stats map, please change the following line of code:
oneOfTheTextViews.text = document.getString("str")
to
oneOfTheTextViews.text = document.getString("stats.str")
If your str property is a number and not a String, then instead of the above line of code please use this one:
oneOfTheTextViews.text = document.getLong("stats.str")
Should I not nest them at all?
No, you can nest as many properties as you want within a Map.
Is there a way to get a list of document names?
Yes, simply iterate the collection and get the document ids using getId() function.

How do I add equal elements to an array in Firestore in Flutter?

I just figured out that in Flutter
List<int> values = [1,2,3,1,2,3];
Firestore.instance.path.updateData({"values": FieldValue.arrayUnion(values)});
results in the Firestore Array
(Firestore Array) [1,2,3]
Obviously the values are somehow mapped, removing duplicate values, although I can manually add duplicate values online in the Firebase panel (the datatype is called array respectively).
Is there a way to bypass this behaviour or is this a bug?
According to the documentation here, array union will only add the elements that are not present. Because you're using a Firestore function intended to only add new unique values, it will behave that way without being bypassed. You can certainly bypass this function, though, but not without first calling the document, retrieving the array, and appending it before updating it to Firestore.
Use
List<int> values = [1,2,3,1,2,3];
Firestore.instance.path.setData({"values": values});
It works without using FieldValue.arrayUnion

What data-structure is this (looks like an Array) and how can I access it? Ruby

I'm connecting to Parse.com using the parse-ruby-client gem.
I'm retrieving some photo data that seems like an array (if I .class it, is tells me array).
But I dont know how to access the data inside:
my_photos = [Photo:hoGXC2uRKf {"image"=>#<Parse::File:123 #parse_filename="name-1", #url="http://...">, "objectId"=>"hoGXC2uRKf", "user"=>#<Parse::Pointer:0x007f97152421e8 #class_name="_User", #parse_object_id="aFqn1XREtp">}, Photo:9GpFS26PsR {...etc]
So how can I get the objectId for the first photo for example?
(without using my_photos[0])
I want to get the data according to objectId
Thanks a lot!
As discussed in the comments, this is an array of Parse::Objects, which appear to be Hash-like. You can access an element of the array by subscripting with an index [0], and a named element of the hash by subscripting with the key ["objectId"].

How to iterate through object array with form helpes in cakephp?

I have an object CuratedPage with property pageName.
I am creating an array of CuratedPage objects in controller and setting it for the view like this:
$this->set('curatedPages', $curatedPages);
In the view I am creating a dropdown of page names like this:
$pageNames = array();
foreach($curatedPages as $curatedPage) {
array_push($pageNames, $curatedPage->getPageName());
}
echo $this->Form->input('curatedPage', array('options' => $pageNames));
Is there a way in cakephp that will allow me to pass the array of CuratedPage objects to the Form->input(...) instead of creating an array of scalar values.
I'm not sure what you would expect the form helper to do in that case. However, depending on your PHP version (>= 5.2.0 required) the magic __toString() method might do it. If you implement it to return the pagename, then you would end up with the same result as with your posted snippet, ie an numerical indexed (the value attribute) HTML option list with the page names as labels.
However, implementing this only for that purpose in this specific view seems wrong to me, you're probably better of utilizing a custom helper, or as #BrianGlaz suggested prepare the data in the controller.

Ruby on rails form for postgresql array

I haven't found any example of how to make a dynamic form for a field which is a Postgresql array.
I want to fill it dynamically using Mustache.js. I have done it before but with a string field using a json string.
I am using ruby2.0, rails4.0 and postgresql9.1.
Thank you very much.
You can insert the Array values into a form with:
<input type = 'textbox' name = 'model[array_column_name][:item_1, :item_2...]'>
params['model']['array_column_name']
I'm not familiar with mustache.js but the above should give you a rough understanding on how to implement a form with a PostgreSQL Array.

Resources