I am having issue with the paginated results as follows :
Request :
GET http://localhost:1000/api/v1/public/blog/articles
Response :
{
"current_page": 1,
"data": [
{
"article_id": 43
},
{
"article_id": 107
},
{
"article_id": 171
},
{
"article_id": 22
},
{
"article_id": 86
},
{
"article_id": 150
},
{
"article_id": 1
},
{
"article_id": 65
},
{
"article_id": 129
},
{
"article_id": 44
}
],
"first_page_url": "http://localhost:1000/api/v1/public/blog/articles?page=1",
"from": 1,
"last_page": 18,
"last_page_url": "http://localhost:1000/api/v1/public/blog/articles?page=18",
"next_page_url": "http://localhost:1000/api/v1/public/blog/articles?page=2",
"path": "http://localhost:1000/api/v1/public/blog/articles",
"per_page": "10",
"prev_page_url": null,
"to": 10,
"total": 179
}
So, now If I request again in the same link, it shows a different set of articles with different ids.
Also If I go to page=1 or page=2, each and every pages the results are inconstant.
I am not sure from where this issue is occuring,
I want to mention that my other paginated queries are working fine except when I am trying to concat queries for getting a specific result as follows :
$query = DB::query();
// Then Concatenating other queries based on options
// Now Execute The Whole Query
$fetched_articles = $query->paginate(5);
It would be helpful, if anyone can describe the paginator flows here with the solution or workarounds.
Thanks in Advance !
According to Laravel documentation
Currently, pagination operations that use a groupBy statement cannot
be executed efficiently by Laravel. If you need to use a groupBy with
a paginated result set, it is recommended that you query the database
and create a paginator manually.
so, you can not use groupBy in your query, if you want pagination.
By the way, you can try this blog post, may be it will work for you.
Related
Here is my code.
$orders = Order::with('customer')->withCount('orderItems')->paginate(50)->toArray();
This code return me the blank page. No data return. So here's what I did wrong?
When you are using Laravel pagination, it simply gives you an array of Data.
You don't need to convert to toArray().Use the following code :
$orders = Order::with('customer')->withCount('orderItems')->paginate(15);
The response of the pagination will be the following :
Content-Type: application/vnd.api+json
{
"meta": {
"page": {
"current-page": 2,
"per-page": 15,
"from": 16,
"to": 30,
"total": 50,
"last-page": 4
}
},
"links": {
"first": "http://localhost/api/v1/posts?page[number]=1&page[size]=15",
"prev": "http://localhost/api/v1/posts?page[number]=1&page[size]=15",
"next": "http://localhost/api/v1/posts?page[number]=3&page[size]=15",
"last": "http://localhost/api/v1/posts?page[number]=4&page[size]=15"
},
"data": [...]
}
I am writing a GraphQL resolver that retrieves all vertices by a particular edge using the following query (created returns label person):
software {
created {
name
}
}
Which would resolve to the following Gremlin Query for each software node found:
g.V().hasLabel('software').has('name', 'ripple').in('created')
This returns a result that includes all properties of the object:
{
"result": [
{
"#type": "d",
"#rid": "#24:0",
"#version": 6,
"#class": "person",
"in_knows": [
"#35:0"
],
"name": "josh",
"out_created": [
"#32:0",
"#33:0"
],
"age": 32,
"#fieldTypes": "in_knows=g,out_created=g"
}
],
"dbStats": {
...
}
}
I realize that this will fall foul on GraphQL's N+1 query so i'm trying to batch queries together using a Dataloader pattern. (i'm also hoping to do property selections, so i'm not asking the database to return too much info)
So i'm trying to craft a query like so:
g.V().union(
__.hasLabel('software').has('name', 'ripple').
project('parent', 'child').by('id').
by(__.in('created').fold()),
__.hasLabel('software').has('name', 'lop').
project('parent', 'child').by('id').
by(__.in('created').fold())
)
But this results in the following where the props are missing and it just includes the id of the vertices I want:
{
"result": [
{
"parent": "ripple",
"child": [
"#24:0"
]
},
{
"parent": "lop",
"child": [
"#22:0",
"#23:0",
"#24:0"
]
}
],
"dbStats": {
...
}
}
My Question is, how can I have the Gremlin query return all of the props for the found vertices and none of the other props? Should I even been doing batching this way?
For anyone else reading, the query I was trying to write wouldn't work because the TraversalSet created in the .by(_.in('created') can't be cast from a List to an ElementMap as the stream cardinality wouldn't be enforced. (You can only have one record per row, I think?)
My working query would be to duplicate the keys for each row and specify the props needed (the query below is ok for gremlin 3.3 as used in ODB, otherwise if you've got < gremlin 3.4 replace the last by step with be(elementMap('name', 'age')):
g.V().union(
__.hasLabel('software').has('name', 'ripple').
as('parent').
in('created').as('child').
select('parent', 'child').
by(values('name')).
by(properties('id', 'name', 'age').
group().by(__.key()).
by(__.value())),
__.hasLabel('software').has('name', 'lop').
as('parent').
in('created').as('child').
select('parent', 'child').
by(values('name')).
by(properties('id', 'name', 'age').
group().by(__.key()).
by(__.value()))
)
So that you get a result like this:
{"data": [
{
"parent": "ripple",
"child": {
"id": 5717,
"name": "josh",
"age": 32
}
},
{
"parent": "lop",
"child": {
"id": 5709,
"name": "peter",
"age": 35
}
},
{
"parent": "lop",
"child": {
"id": 5713,
"name": "marko",
"age": 29
}
},
{
"parent": "lop",
"child": {
"id": 5717,
"name": "josh",
"age": 32
}
}
]
}
Which would allow you to create a lookup where you concat all results for "lop" and "ripple" into arrays.
I am using the url below to fetch the products in a particular category and it works fine.
/rest/default/V1/products?searchCriteria[filterGroups][0][filters][0][field]=category_id&searchCriteria[filterGroups][0][filters][0][value]=262&searchCriteria[filterGroups][0][filters][0][conditionType]=eq&searchCriteria[filterGroups][1][filters][0][field]=visibility&searchCriteria[filterGroups][1][filters][0][value]=4&searchCriteria[filterGroups][1][filters][0][conditionType]=eq&searchCriteria[pageSize]=10&searchCriteria[currentPage]=0
The only issue is that I would like to show filters based on the categories selected. For eg a price filter works for every category but a size and color filter would only work for clothing category while screen size would work for electronics.
Any idea on how to fetch the filters for each category?
I found the answer ,but I had to switch from using rest api to graphql as graphql supports aggregations . This was exactly what I needed and also seems like graphql has more features as compared to rest api for magento 2.
You can try magento search api to get filters by passing category or any other field like below example.
{{url}}/rest/V1/search?searchCriteria[requestName]=catalog_view_container&searchCriteria[filterGroups][0][filters][0][field]=category_ids&searchCriteria[filterGroups][0][filters][0][value]=10
Result will have category and other buckets, something like below:
[
{
"name": "category_bucket",
"values": [
{
"value": "2",
"metrics": [
2,
634
]
},
{
"value": "10",
"metrics": [
10,
634
]
}
]
},
{
"name": "brand_bucket",
"values": [
{
"value": "617",
"metrics": [
617,
562
]
},
{
"value": "639",
"metrics": [
639,
29
]
},
{
"value": "1218",
"metrics": [
1218,
26
]
},
{
"value": "640",
"metrics": [
640,
8
]
},
{
"value": "1332",
"metrics": [
1332,
4
]
}
]
}
]
You can map this result with attribute api result to get label
{{url}}/rest/all/V1/products/attributes?searchCriteria=&fields=items[attribute_id,attribute_code,options,frontend_labels]
I'm developing an API, and for index routes, I return paginated but now I should using eloquent resources, but pagination JSON structure of these two are different and front-end code doesn't work anymore.
my codes are:
// AdminUserController.php
public function index()
{
return User::paginate();
}
// ClientUserController.php
public function index()
{
return new UserCollection(User::paginate());
}
the first JSON structure like :
{
"total": 50,
"per_page": 15,
"current_page": 1,
"last_page": 4,
"next_page_url": "http://my.app/api/admin/users?page=2",
"prev_page_url": null,
"from": 1,
"to": 15,
"data": [
{
// Result Object
},
{
// Result Object
}
]
}
But the second is like:
{
"data": [
{
// Result Object
},
{
// Result Object
}
],
"links":{
"first": "http://my.app/api/app/users?page=1",
"last": "http://my.app/api/app/users?page=2",
"prev": null,
"next": null
},
"meta":{
"current_page": 1,
"from": 1,
"last_page": 2,
"path": "http://my.app/api/admin/users",
"per_page": 15,
"to": 15,
"total": 25
}
}
why laravel paginated responses have different JSON structure?
What does your UserCollection class look like?
At first glance, it looks like this is because the first method AdminUserController#index is converting the response to JSON, while the second method ClientUserController #index is returning a collection.
I'm trying to use YQL Console to get currency rates, the YQL statement is
select * from yahoo.finance.xchange where pair in ("EURUSD","GBPUSD")
the console results give me
{
"query": {
"count": 2,
"created": "2017-10-26T02:42:44Z",
"lang": "en-US",
"results": {
"rate": [
{
"id": "EURUSD",
"Name": "EUR/USD",
"Rate": "1.1829",
"Date": "10/26/2017",
"Time": "3:42am",
"Ask": "1.1829",
"Bid": "1.1829"
},
{
"id": "GBPUSD",
"Name": "GBP/USD",
"Rate": "1.3269",
"Date": "10/26/2017",
"Time": "3:42am",
"Ask": "1.3269",
"Bid": "1.3269"
}
]
}
}
}
but the rest query gives me error
{"error":{"lang":"en-US","diagnostics":{"cache":{"execution-start-time":"0","execution-stop-time":"0","execution-time":"0","method":"GET","type":"MEMCACHED","content":"ENV.queryyahooapiscomproductionsg3.store://datatables.org/alltableswithkeys.15a841ff462a38eb6175e73b4dc747ef"},"env":"Failed to read from storage: store://datatables.org/alltableswithkeys: Invalid store url: store://datatables.org/alltableswithkeys","warning":"Invalid environment specified: store://datatables.org/alltableswithkeys"},"description":"No definition found for Table yahoo.finance.xchange"}}
The yahoo.finance.xchange is a community table. In the YQL console there should be a checkbox saying Show Community Tables select that and you should have access to it. The REST call here works. Let me know if you have any questions.