Join query in Laravel 5.6 - eloquent

I have a Join query like below in Laravel 5.6.
->join('employee', function ($join) {
$join->on('company.surah_id', '=', 'employee.surah_id');
$join->on('company.verse_id', '=', 'employee.ayah_id');
})
I have a field text in employee table. I would like to fetch certain amount of character. More over I would like to fetch like below
"employee": {
"text": "Jamal"
}

Related

Sort by json element at nested level for jsonb data - postgresql

I have below table in postgresql which stored JSON data in jsonb type of column.
CREATE TABLE "Trial" (
id SERIAL PRIMARY KEY,
data jsonb
);
Below is the sample json structure
{
"id": "000000007001593061",
"core": {
"groupCode": "DVL",
"productType": "ZDPS",
"productGroup": "005001000"
},
"plants": [
{
"core": {
"mrpGroup": "ZMTS",
"mrpTypeDesc": "MRP",
"supLeadTime": 777
},
"storageLocation": [
{
"core": {
"storageLocation": "H050"
}
},
{
"core": {
"storageLocation": "H990"
}
},
{
"core": {
"storageLocation": "HM35"
}
}
]
}
],
"discriminator": "Material"
}
These are the scripts for insert json data
INSERT INTO "Trial"(data)
VALUES(CAST('{"id":"000000007001593061","core":{"groupCode":"DVL","productType":"ZDPS","productGroup":"005001000"},"plants":[{"core":{"mrpGroup":"ZMTS","mrpTypeDesc":"MRP","supLeadTime":777},"storageLocation":[{"core":{"storageLocation":"H050"}},{"core":{"storageLocation":"H990"}},{"core":{"storageLocation":"HM35"}}]}],"discriminator":"Material"}' AS JSON))
INSERT INTO "Trial"(data)
VALUES(CAST('{"id":"000000000104107816","core":{"groupCode":"ELC","productType":"ZDPS","productGroup":"005001000"},"plants":[{"core":{"mrpGroup":"ZCOM","mrpTypeDesc":"MRP","supLeadTime":28},"storageLocation":[{"core":{"storageLocation":"H050"}},{"core":{"storageLocation":"H990"}}]}],"discriminator":"Material"}' AS JSON))
INSERT INTO "Trial"(data)
VALUES(CAST('{"id":"000000000104107818","core":{"groupCode":"DVK","productType":"ZDPS","productGroup":"005001000"},"plants":[{"core":{"mrpGroup":"ZMTL","mrpTypeDesc":"MRP","supLeadTime":28},"storageLocation":[{"core":{"storageLocation":"H050"}},{"core":{"storageLocation":"H990"}}]}]}' AS JSON))
If try to sort by at first level then it works
select id,data->'core'->'groupCode'
from "Trial"
order by data->'core'->'groupCode' desc
But when I try to sort by at nested level, below is the script then it doesn't work for me, I'm for sure I'm wrong for this script but don't know what is it ? Need assistant if someone knows how to order by at nested level for JSONB data.
select id,data->'plants'
from sap."Trial"
order by data->'plants'->'core'->'mrpGroup' desc
Need assistance for write a query for order by at nested level for JSONB data.
Below query works for me
SELECT id, data FROM "Trial" ORDER BY jsonb_path_query_array(data, '$.plants[*].core[*].mrpGroup') desc limit 100

Return aggregated data from federated query

I am working with SPARQL federated queries. I do not wish to return anything other than aggregated data from each service query. Is this possible? Currently, the query returns data from each service query, and then runs a count and group by. I tried to use subqueries, but they do not work. I am using a fuseki server.
<Edited - added sample query>
SELECT (COUNT(?var) as ?varCount) ?var2
WHERE{
{
?var a abc:Class;
}
UNION
{
SERVICE <https://sample1.org>
{
?var a abc:Class;
?var abc:var2 ?var2.
}
}
UNION
{
SERVICE <https://sample2.org>
{
?var a abc:Class;
?var abc:var2 ?var2.
}
}
} group by ?var2

Avoiding Hasura sequential scan for query on large array relation

How do I get Hasura to generate SQL that uses the index on review.user_id?
Here is the seemingly obvious way of getting a user's reviews. I've included a simplified version of the SQL that Hasura generates, which is a sub query approach rather than a JOIN of the review table on user:
# SELECT * FROM review
# WHERE user_id = (
# SELECT id FROM "user" WHERE username = 'admin'
# )
# LIMIT 50
# This uses a sequential scan on `review` because Postgres
# can't know exactly what the subquery returns.
query ReviewsForUserSlow {
user(where: { username: { _eq: "admin" } }) {
reviews(limit: 50) {
text
}
}
}
Here is a hack to get Hasura to generate SQL that does indeed use the review.user_id index. However, the caveat is that we need to make a round trip to Hasura to get the user ID in order to build this query:
# Simplification of the SQL Hasura generates:
# SELECT * FROM review
# WHERE user_id = '8f3547e4-c8a9-480f-991f-0798c02f2ba2'
# LIMIT 50
query ReviewsForUserFast {
review(
limit: 50,
where: {
user_id: {
_eq: "8f3547e4-c8a9-480f-991f-0798c02f2ba2"
}
}
) {
text
}
}

Postgres find in jsonb nested array

I have a case when my data in in nested arrays of jsonb in order to find the value I have to do multiple JSONB_ARRAY_ELEMENTS which is costly and takes a lots of nested code.
The json file has the continents inside countries and inside cities.
I need to access a city value.
Is there a way to make this query simpler and faster?
I was trying to solve it using JSON_EXTRACT_PATH but in order to get in to a array but I need the indexes.
WITH mydata AS (
SELECT '
{
"continents":[
{
"name":"America",
"area":43316000,
"countries":[
{
"country_name":"Canada",
"capital":"Toronto",
"cities":[
{
"city_name":"Ontario",
"population":2393933
},
{
"city_name":"Quebec",
"population":12332
}
]
},
{
"country_name":"Brazil",
"capital":"Brasilia",
"cities":[
{
"city_name":"Sao Paolo",
"population":34534534
},
{
"city_name":"Rio",
"population":445345
}
]
}
]
},
{
"name":"Europa",
"area":10530751,
"countries":[
{
"country_name":"Switzerland",
"capital":"Zurich",
"cities":[
{
"city_name":"Ginebra",
"population":4564565
},
{
"city_name":"Basilea",
"population":4564533
}
]
},
{
"country_name":"Norway",
"capital":"Oslo",
"cities":[
{
"city_name":"Oslo",
"population":3243534
},
{
"city_name":"Steinkjer",
"population":4565465
}
]
}
]
}
]
}
'::JSONB AS data_column
)
SELECT cit.city->>'city_name' AS city,
(cit.city->>'population')::INTEGER AS population
FROM (SELECT JSONB_ARRAY_ELEMENTS(coun.country->'cities') AS city
FROM (SELECT JSONB_ARRAY_ELEMENTS(cont.continent->'countries') AS country
FROM (SELECT JSONB_ARRAY_ELEMENTS(data_column->'continents') AS continent
FROM mydata
) AS cont
WHERE cont.continent #> '{"name":"Europa"}'
) AS coun
WHERE coun.country #> '{"country_name" : "Norway"}'
) AS cit
WHERE cit.city #> '{"city_name": "Oslo"}'
See my nested queries? looks ugly, I can get the answer using: JSONB_EXTRACT_PATH( data_column->'continents', '1', 'countries', '1', 'cities', '0', 'population') but I had to hardcode the array indexes.
Hope you can help me out.
Thanks.
You don't need any nesting, you can do lateral queries:
SELECT
city->>'city_name' AS city,
(city->>'population')::INTEGER AS population
FROM
mydata,
JSONB_ARRAY_ELEMENTS(data_column->'continents') AS continent,
JSONB_ARRAY_ELEMENTS(continent->'countries') AS country,
JSONB_ARRAY_ELEMENTS(country->'cities') AS city
WHERE continent ->> 'name' = 'Europa'
AND country ->> 'country_name' = 'Norway'
AND city ->> 'city_name' = 'Oslo';
(online demo)
However, since you mentioned paths and having to specify indices in there, this is actually the perfect use case for Postgres 12 JSON paths:
SELECT jsonb_path_query(data_column, '$.continents[*]?(#.name == "Europa").countries[*]?(#.country_name=="Norway").cities[*]?(#.city_name=="Oslo")') FROM mydata
(online demo)

How to Convert Mysql Query into Laravel(Mongodb) Query?

here, This Query is Perfectly Run in Mysql.
but Overwrite this Query into Laravel(Mongodb) Query Format
SELECT F.status, U.username, U.email
FROM users U, friends F
WHERE
CASE
WHEN F.friend_one = '$user_id'
THEN F.friend_two = U.user_id
WHEN F.friend_two= '$user_id'
THEN F.friend_one= U.user_id
END
AND
F.status='1';
this is friend module and get the my all friend list
here is my table look like
{
"_id" :"5a12618b82956d4c0e00002d",
"otheruser" : "598442b58ed7bc0c19000029",
"relation_type" : 2,
"is_friend" : "true",
"user_id" : "59bf784682956df00c00002a"
}
here is my sample query in repository..
public function GetFriendsOfUser($id,$userId)
{
$list = $this->makeModel()
->with('getallfrienduser')
->where('is_friend',"true")
->where('relation_type',2)
->where(function ($query) use ($id,$userId) {
$query->where('user_id', $userId)
->orWhere('otheruser', $id);
})
->get();
}
here sample join in model and join are sapreated
public function getallfrienduser()
{
return $this->hasMany('App\Models\Users','_id','user_id');
}
public function getallfriendother()
{
return $this->hasMany('App\Models\Users','_id','otheruser');
}
but only one join use(user_id,otheruser) alternatively record find two user.