How to retrieve Json data type data in SQL using a where condition in LARAVEL - laravel

Question : How to retrieve Json data type data in SQL using a where condition in LARAVEL?
I want to display all the order that contains order->Product->user->id === 1
{
"currentUserID": 1,
"currentUserName": "Mohamed Naalir",
"order": [
{
"id": 26,
"Product": [
{
"id": 4,
"name": "Araliya Rice",
"desription": "Araliya Rice",
"salePrice": 500,
"category": "Rice",
"user": {
"id": 1,
"name": "Mohamed Naalir",
}
}
],
},
{
"id": 27,
"Product": [
{
"id": 2,
"name": "white sugar",
"desription": "aaa",
"salePrice": 100,
"category": "Sugar",
"user": {
"id": 5,
"name": "Mohamed Sharaf",
}
}
],
}
]
}

json where clauses
$orders = DB::table('orders')
->whereJsonContains('Product', [['user' => ['id' => 1]]])
->get();

Related

Filter documents out of the facet count in enterprise search

We use enterprise search indexes to store items that can be tagged by multiple tenants.
e.g
[
{
"id": 1,
"name": "document 1",
"tags": [
{ "company_id": 1, "tag_id": 1, "tag_name": "bla" },
{ "company_id": 2, "tag_id": 1, "tag_name": "bla" }
]
}
]
I'm looking to find a way to retrieve all documents with only the tags of company 1
This request:
{
"query": "",
"facets": {
"tags": {
"type": "value"
}
},
"sort": {
"created": "desc"
},
"page": {
"size": 20,
"current": 1
}
}
Is coming back with
...
"facets": {
"tags": [
{
"type": "value",
"data": [
{
"value": "{\"company_id\":1,\"tag_id\":1,\"tag_name\":\"bla\"}",
"count": 1
},
{
"value": "{\"company_id\":2,\"tag_id\":1,\"tag_name\":\"bla\"}",
"count": 1
}
]
}
],
}
...
Can I modify the request in a way such that I get no tags by "company_id" = 2 ?
I have a solution that involves modifying the results to strip the extra data after they are retrieved but I'm looking for a better solution.

Laravel groupBy when date does not exist

I'm running the code:
DowntimeHistoryMonitoring::whereDate('created_at', '<=', now())->get(['id', 'created_at'])->groupBy(function ($item){
return $item->created_at->format('Y-m-d');
});
Which returns:
{
"2022-11-30": [
{
"id": "507a3832-c9d3-4baf-b6a8-ff8100c59897",
"created_at": "2022-11-30T13:06:42.000000Z"
},
{
"id": "507a3832-c9d3-4baf-b6a8-ff8100c59897",
"created_at": "2022-11-30T14:06:42.000000Z"
}
],
"2022-11-29": [
{
"id": "507a3832-c9d3-4baf-b6a8-ff8100c59897",
"created_at": "2022-11-29T13:06:42.000000Z"
}
]
}
How can I do so that I get an empty result if I don't find records, and defined by a limit within the groupBy. Example with a limit of 5:
{
"2022-11-30": [
{
"id": "507a3832-c9d3-4baf-b6a8-ff8100c59897",
"created_at": "2022-11-30T13:06:42.000000Z"
},
{
"id": "507a3832-c9d3-4baf-b6a8-ff8100c59897",
"created_at": "2022-11-30T14:06:42.000000Z"
}
],
"2022-11-29": [
{
"id": "507a3832-c9d3-4baf-b6a8-ff8100c59897",
"created_at": "2022-11-29T13:06:42.000000Z"
}
],
"2022-11-28": [
],
"2022-11-27": [
],
"2022-11-26": [
],
}
What should I do?

Laravel nested eloquent relationships using model

I am trying to getting the more sorted object as output using laravel eloquent relationships
Section::with(['fieldTypes','fields'=>
function ($query) use ($id) {
$query->where('user_id', '=', $id);
},
'fields.fieldType'=> function ($query) {
$query->select('id','type','url','icon');
}])->get();
Output
"fields": [
{
"id": 1,
"user_id": 1,
"field_type_id": 1,
"section_id": 3,
"title": "Facebook",
"value": "inderjit",
"order": 1,
"field_type": {
"id": 1,
"type": "facebook",
"url": "https://www.facebook.com/"
}
}
]
I want output like this . I want to merge 2 objects as one
"fields": [
{
"id": 1,
"user_id": 1,
"field_type_id": 1,
"section_id": 3,
"title": "Facebook",
"value": "inderjit",
"order": 1,
"type": "facebook",
"url": "https://www.facebook.com/"
}
]
Something like this. You can add a method in your model where you trigger a leftJoin. Then you have the fields of the relation in the model.
public function scopeJoinWithFieldTypes($query)
{
return $query->leftJoin("field_types", "field_type.id", "=", "fields.id");
}

How to get Order and OrderLineItem for a transaction using Connect V2?

I am writing a wrapper to retrive transaction data for the Square Connect API V2. I am able to retrive the trasactions with Order data missing.
I am getting following response using All Transaction and Retrieve Transaction API:
{
"transactions": [
{
"id": "mYziFkYv2QK7e2kb2vyIhegeV",
"location_id": "75S3K9Z9KSVYK",
"created_at": "2017-04-17T11:00:51Z",
"tenders": [
{
"id": "2qeDw6CmCs299m9w0RY7KQB",
"location_id": "75S3K9Z9KSVYK",
"transaction_id": "mYziFkYv2QK7e2kb2vyIhegeV",
"created_at": "2017-04-17T11:00:51Z",
"amount_money": {
"amount": 10000,
"currency": "INR"
},
"processing_fee_money": {
"amount": 0,
"currency": "INR"
},
"type": "OTHER"
}
],
"product": "REGISTER",
"client_id": "75S3K9Z9KSVYK-a776-4377-84f5-75S3K9Z9KSVYK"
},
{
"id": "UJsg9IdIv9WWvqT1h2VkbxgeV",
"location_id": "75S3K9Z9KSVYK",
"created_at": "2017-04-17T11:00:37Z",
"tenders": [
{
"id": "UVuQghb8RTF8OUcmAsaXKQB",
"location_id": "75S3K9Z9KSVYK",
"transaction_id": "UJsg9IdIv9WWvqT1h2VkbxgeV",
"created_at": "2017-04-17T11:00:37Z",
"amount_money": {
"amount": 0,
"currency": "INR"
},
"processing_fee_money": {
"amount": 0,
"currency": "INR"
},
"type": "NO_SALE"
}
],
"product": "REGISTER",
"client_id": "75S3K9Z9KSVYK-a751-4434-a041-75S3K9Z9KSVYK"
}
]}
Is there any way to get order (line item) details?
If you are looking for itemizations, you can use the v1 transactions endpoints.
See here: https://docs.connect.squareup.com/api/connect/v1/#get-paymentid

Elasticsearch - How to get filtered response without harming the links of each entity/field

So assuming that i have a mapping structure like the following
{
"mappings": {
"users": {
"properties": {
"user": {
"type": "nested"
}
}
}
}
}
and I have indexed the following
users/52
{
"user": [
{
"id": 52,
"first": "John",
"last": "Smith",
"age": 21,
"school": {
"name": "STC",
"location": "Mt LV",
"District": "Western"
}
}
]
}
users/57
{
"user": [
{
"id": 57,
"first": "Alice",
"last": "White",
"age": 25,
"school": {
"name": "HFC",
"location": "DEH WLA",
"District": "Western"
}
}
]
}
What if I want to get certain fields using the id and without destroying the relationship link of each other.
For an example
If id == 57
then the return structure should consists only "first","age","school.name","school.District"
{
"user": [
{
"first": "Alice",
"age": 25,
"school": {
"name": "HFC",
"District": "Western"
}
}
]
}
How should you write a query for this sort of response in Elasticsearch?
Use response filtering in Elasticsearch. According to your scenario, a GET request would look like GET /_search?user=57&filter_path=first,age,school.name,school.District

Resources