Apiary: Refer to multiple models in a body - apiblueprint

It seems you can only refer to the model itself in an unmodified form from a payload/body section:
# Item [/]
+ Model
{ "name" : "an item" }
## GET
+ Response 200
[Item][]
Is there any way to return an array of the model or use the model as a field? I.e. something like
+ Response 200
{ "items" : [ [Item][], [Item][] ]}

This is not possible yet.
It should be available soon using Traits.

Related

How to convert lavavel collection to square bracket collection

I have laravel livewire collection as below.
[
{
"date":"2021.09.01-0:00",
"open":110.177,
"close":110.175,
"low":110.172,
"high":110.18
},
{
"date":"2021.09.01-0:01",
"open":110.175,
"close":110.171,
"low":110.169,
"high":110.175
},
{
"date":"2021.09.01-0:02",
"open":110.171,
"close":110.173,
"low":110.17,
"high":110.176
}
]
I would like to convert them into form of square bracket collection without key name as below .
$data = [
['2021.09.01-0:00',110.177,110.175,110.172,110.18],
['2021.09.01-0:01',110.175,110.171,110.169,110.175],
['2021.09.01-0:02',110.171,110.173,110.17,110.176]
];
Any advice or guidance on this would be greatly appreciated, Thanks.
You can use collection map method:
The map method iterates through the collection and passes each value to the given callback. The callback is free to modify the item and return it
https://laravel.com/docs/8.x/collections#method-map
$expectData = $collection->map(fn($item) => [$item-> date, $item->open,...])
I am not sure what the original data is but you can map over the collection and pull the values; if Eloquent Models:
$data->map(fn ($v) => array_values($v->toArray()))
Would depend upon how you got that original Collection for how it would end up in this scenario.
If you need to restrict what attributes and the order of them returned you can use only on the model:
fn ($v) => array_values($v->only(['date', ...]))

laravel reading object file

![my collection is like][1]
foreach ($collection['documents'] as $user) {
$age = array("name"=>$user['data1'],"gender"=>$user['data3'],"martial"=>$user['data4'],"education"=>$user['data5'],"home"=>$user['data6'],
"phone"=>$user['data14'],"date"=>$user['data15'],"record"=>$user['data71'],"status"=>$user['data75'],"totalpoint"=>$user['data73'],"monitorypoint"=>$user['data74']);
$data[]=$age;
}
Error :
Cannot use object of type MrShan0\PHPFirestore\FirestoreDocument as array
Edit
when i try like this
foreach ($collection['documents'] as $user) {
dd($user->fields);
}
Cannot access private property MrShan0\PHPFirestore\FirestoreDocument::$fields
[1]: https://i.stack.imgur.com/7IvqS.png
You are treating Collections and Documents as arrays instead of objects (as in JSON), thus accessing them in the wrong way. For instance, take a look on the Firestore Data Model:
For example, you could have a users collection to contain your various users, each represented by a document:
users
|___alovelace
| |___first : "Ada"
| |___last : "Lovelace"
| |___born : 1815
|___aturing
|___first : "Alan"
|___last : "Turing"
|___born : 1912
According to the documentation, if you want (for example) to access multiple documents within your collection:
$citiesRef = $db->collection('samples/php/cities');
$documents = $citiesRef->documents();
foreach ($documents as $document) {
// blabla
}
Then, you can access fields within a document (DocumentSnapshot) as an array:
// One specific field wallet->cryptoCurrency->bitcoin
$bitcoinWalletValue = $snapshot['wallet']['cryptoCurrency']['bitcoin'];
See more on collection(), documents(), document(), data().
IMPORTANT
Please note that this is not an exact implementation of what you should do as I don't have enough context/information on your database structure. This is just to provide you some guidance.

Spring Data Rest PATCH add to embedded list

I'm having difficulty adding to an embedded list using Spring Data Rest and a PATCH request. I'm using MongoDB so no JPA joins (ManyToOne etc) here, just a plain old regular embedded List of child type.
My beans look like this:
class Parent {
String name;
List<Child> children;
}
class Child {
String name;
}
My request looks like this:
curl -d '{"children": [ {"name": "bob"} ] }' -H "Content-Type: application/json" -X PATCH http://localhost:8080/api/parent/123
The result of this is that all the child elements are replaced with the new one from the request, e.g.
old: [ 'tom', 'sally' ]
request: [ 'bob' ]
expected result: [ 'tom', 'sally', 'bob']
actual result: [ 'bob' ]
I've stepped through the Spring code (DomainObjectReader) and it just doesn't seem to handle my scenario but surely this is a really simple use case, any ideas? Am I missing something obvious?
Thanks!
The problem is tha you try to edit the parent object, instead of the relationship collection.
You need to create a child:
POST /api/child {...}
which returns a url to the newly created child (either in the location header or as a self link in the response - based on your SDR settings)
Then add this child to the parent's children collection:
PATCH /api/parent/123/children
Content-Type:text/uri-list
body: ***the URI of the child***

How to join results from with operator in Laravel?

I use query builder for execution inquiries to db like as:
$result = Order::with("product", "images");
As result I get response with two nested objects inside:
{["product" : [{}], "images" : [{}]]}
How can I join these two objects to one, that to get the following response:
{"title" : "Product name", "price" : 3, "images" :{}, "order" : {} }
You should use nested eager loading here:
Order::with('products.images')->first();
If you want to get an array or JSON instead of collection, use toArray() or toJson() methods on collection.

Using a combined field as id mapping in ElasticSearch

From this question I can see that it is possible Use existing field as id in elasticsearch
My question is, if can do similar thing but concatenating fields.
{
"RecordID": "a06b0000004SWbdAAG",
"SystemModstamp": "01/31/2013T07:46:02.000Z",
"body": "Test Body"
}
And then do something like
{
"your_mapping" : {
"_id" : {
"path" : "RecordID" + "body"
}
}
}
So the id is automatically formed from concatenating those fields.
No you can't, you can only make the _id point to a field that's within the document, using the dot notation as well if needed (e.g. level1,level2.id).
I'd suggest to have a field that contains the whole id in your documents, or even better to take the id out and provide it in the url, as configuring a path causes the document to be parsed when not needed.

Resources