How to make dynamic query in laravel 5.3? - laravel

What exactly I wanna do is, I want to make dynamic query in laravel 5.3 based on requested parameters , so in request i will get column names then filters for that query and I don't know tables from which I want to process the data.
So, my question is how to decide the tables for that query ?
or should I store table and respective columns in one database's table and match the requested parameters with that table so that I will get table name and will able to put in that query?
But I thought this will cost my processing ? so that's why I post this question. please help me with best scenario that will fit with my requirment for dynamic query?
Update
the request will be like this
{
"col": ['fname', 'lname'],
"offset": 1,
"limit": 25,
"order": [ASC, fname, lname],
"filter": [
{
"col": "id",
"op": "=",
"val": 8
}
]
}
so this is my request and table name and related columns are in one table.

Just use query builders.
$query = DB::table($tableName);
// ...some logic...
foreach ($filters as $filter) {
$query->where($filter['col'], $filter['op'], $filter['val']);
}
// ...more logic...
if (isset($limit)) {
$query->limit($limit);
}
if (isset($columns)) {
// get desired columns
$records = $query->get($columns);
} else {
$records = $query->get();
}

Related

How to handle where clause in GraphQL Schema

I am new to GraphQL and creating a API Server using Flask and GraphQL,
Facing some issues while handling the "where" clause in GraphQL Request.
The basic Request and Response is working fine . please find a short snippet of the Schema I have designed
type data {
edges:[data_edges]
}
type QueryCustom {
data: data
}
type Query {
query: QueryCustom
}
Below mentioned basic request (Without the where clause) is working fine with this schema
query {
query {
data {
edges { .... }
But Getting an error when I am executing the Request with the where clause
query dataClosingSoon($month: Long) {
query{
data(where: { LastModifiedDate: { CALENDAR_MONTH: { value: { eq: $month } } } } ) {
edges { ....... }
Following is the response I get:
{
"errors": [
{
"locations": [
{
"column": 40,
"line": 1
}
],
"message": "Unknown type 'Long'."
},
{
"locations": [
{
"column": 9,
"line": 5
}
],
"message": "Unknown argument 'where' on field 'QueryCustom.data'."
}
]
}
I need to understand how to handle the where condition.
GraphQL is not SQL. You cannot use SQL clauses such as WHERE, LIKE, etc. in a GraphQL query.
You need to look at the schema to check how can you filter your query. These filters are pre-defined in the schema. You cannot create custom filters (at least in a basic sense) for a GraphQL query.
Edit:
If you want to use the query you are trying to send, your schema should look like something this:
type data {
edges:[data_edges]
}
type Query {
data(where: Filter!): data
}
input type Filter {
lastModifiedDate: // the type of this field
// Rest of the input fields
}
Note that your first query and the second query are totally different. Your second query is clearly wrong due to two reasons:
The Query type does not have a field called data. It only has one field called query. (I wouldn't add a field named query under the Query type though).
Your data field does not have any inputs. But your document (the GraphQL request) clearly does.

Directus - Retrieve relations with GraphQL

I try to retrieve the relations of the database of my directus app.
I use GraphQL with following endpoint https://myapp.directus.app/graphql.
Using a query for the relations according to the docs
query {
relations {
collection
field
}
}
leads to an validation error:
{
"errors": [
{
"message": "GraphQL validation error.",
"extensions": {
"code": "GRAPHQL_VALIDATION_EXCEPTION",
"graphqlErrors": [
{
"message": "Cannot query field "relations" on type "Query". Did you mean "locations"?",
"locations": [
{
"line": 2,
"column": 2
}
]
}
]
}
}
]
}
I use Postman so far and started with a query for the database entries
query {
boards {
id
columns {
id
name
}
}
}
which works as expected.
What I find interesting is, that Postman shows an mouseover text on the keyword "relations" of the 2nd query -->
"Cannot query field relations on type Query. Did you mean locations?"
But unfortunately I have no idea what to do with that message.
Any ideas what is wrong here?
Meanwhile I found the problem. The endpoint for queries on relations is https://myapp.directus.app/graphql/system
With that the query
query {
relations {
collection
field
}
}
is working

Postgres JSONB Array Query

How can I use the jsonb query functions to get matching TrackingDetails from the whole table, when the jsonb field in my table contains json in this format? For example, I have an order_events table, with a column called event_data, that will contain this json:
{
"Responses": [
{
"Response": {
"TrackingDetails": {
"IntegrationId": "IntegrationId",
"MessagePart": 0,
"MessageTotal": 0,
"MessageGroupId": "MessageGroupId",
"SequenceNumber": "SequenceNumber",
"InterfaceRecordId": "InterfaceRecordId",
"SalesOrderNumber": "SalesOrderNumber",
"SalesOrderReference": "SalesOrderReference",
"DispatchNumber": "DispatchNumber",
"Courier": "Courier",
"TrackingNumber": "TrackingNumber",
"TrackingUrl": "TrackingUrl"
}
}
}
]
}
I would like to get the TrackingDeatils as a Json node, by TrackingNumber, across all rows in the table. It would include multiple rows matched. Thanks for any help.

how to groupBY using spring data

hi i'm using spring data in My project and I'm trying group by two fields, heres the request:
#Query( "SELECT obj from Agence obj GROUP BY obj.secteur.nomSecteur,obj.nomAgence" )
Iterable<Agence> getSecteurAgenceByPc();
but it doesnt work for me..what i want is this result:
-Safi
-CTM
CZC1448YZN
2UA13817KT
-Rabat
-CTM
CZC1349G1B
2UA0490SVR
-Agdal
G3M4NOJ
-Essaouira
-CTM
CZC1221B85
-Gare Routiere Municipale
CZC145YL3
What I get is
{
"status": 0,
"data":
[
{
"secteur": "Safi",
"agence": "CTM"
},
{
"secteur": "Safi",
"agence": "Dep"
},
{
"secteur": "Rabat",
"agence": "Agdal"
},
{
"secteur": "Rabat",
"agence": "CTM"
},
{
"secteur": "Essaouira",
"agence": "CTM"
},
{
"secteur": "Essaouira",
"agence": "Gare Routiere Municipale"
}
]
}
What you want is not possible with JPQL.
What does Group By do?
It combines all rows that are identical in the columns in the group by clause in to one row. Since it combines multiple rows into one, data in other columns can only be present in some combined fashion. For example, you can include MIN/MAX or AVG values, but never the orginal values.
Also the result with always be a table, never a tree.
Also note: there is no duplicated data. Every combination of secteur and agence appears exactly once.
If you want a tree structure, you have to write some java code for that.

couchDB- complex query on a view

I am using cloudantDB and want to query a view which looks like this
function (doc) {
if(doc.name !== undefined){
emit([doc.name, doc.age], doc);
}
what should be the correct way to get a result if I have a list of names(I will be using option 'keys=[]' for it) and a range of age(for which startkey and endkey should be used)
example: I want to get persons having name "john" or "mark" or "joseph" or "santosh" and lie between age limit 20 to 30.
If i go for list of names, query should be keys=["john", ....]
and if I go for age query should use startkey and endkey
I want to do both :)
Thanks
Unfortunately, you can't do so. Using the keys parameter query the documents with the specified key. For example, you can't only send keys=["John","Mark"]&startkey=[null,20]&endkey=[{},30]. This query would only and ONLY return the document having the name John and Mark with a null age.
In your question you specified CouchDB but if you are using Cloudant, index query might be interesting for you.
You could have something like that :
{
"selector": {
"$and": [
{
"name": {
"$in":["Mark","John"]
}
},
{
"year": {
"$gt": 20,
"$lt": 30
}
}
]
},
"fields": [
"name",
"age"
]
}
As for CouchDB, you need to either separate your request (1 request for the age and 1 for the people) or you do the filtering locally.

Resources