Laravel Left Join ( get particular field ) and Group by ID - mysql

Using Laravel 8.12
Unable to get Printer title details in Child Array.
Order Table :
id, name, printer_id
printer Table :
id, title
Resources Table :
id. name
$resources = Resource::with('orders')
->leftJoin('orders', 'resources.id', '=', 'orders.resource_id')
->leftJoin('printers', 'orders.printer_id', '=', 'printers.id')
->select('resources.*')
->groupBy('resources.id');
Using this code i am getting below result. But missing "Title" in child array.
Current Output Result
[
{
"id": 2,
"title": "user 2",
"orders": [
{
"id": 190,
"resource_id": "2",
"printer_id": 2
},
{
"id": 193,
"resource_id": "2",
"printer_id": 3
},
]
},
{
"id": 3,
"title": "user 3",
"orders": [
{
"id": 207,
"resource_id": "3",
"printer_id": 3
},
]
},
{
"id": 4,
"title": "user 4",
"orders": [
{
"id": 466,
"resource_id": "4",
"printer_id": 4,
},
{
"id": 370,
"resource_id": "4",
"printer_id": 5,
}
]
}
]
Required Result
[
{
"id": 2,
"orders": [
{
"id": 190,
"resource_id": "2",
"title": "user 2",
"printer_id": 2
},
{
"id": 193,
"resource_id": "2",
"title": "user 3",
"printer_id": 3
},
]
},
{
"id": 3,
"orders": [
{
"id": 207,
"title": "user 3",
"resource_id": "4",
"printer_id": 3
},
]
},
{
"id": 4,
"orders": [
{
"id": 466,
"title": "user 4",
"resource_id": "4",
"printer_id": 4,
},
{
"id": 370,
"title": "user 5",
"resource_id": "4",
"printer_id": 5,
}
]
}
]
Missing Printer table "title" in child array.

You can simply run
$resources = Resource::with('orders.printer')->paginate(6);
an you will get all the data you need
[
{
"id": 2,
"title": "user 2",
"orders": [
{
"id": 190,
"resource_id": "2",
"printer_id": 2,
"printer" : {
"id" : 2,
"title" : "user 2"
}
},
{
"id": 193,
"resource_id": "2",
"printer_id": 3,
"printer" : {
"id" : 3,
"title" : "user 3"
}
}
]
}
]
Edit
You need the printer() relation declared in the model Order::class
class Order extends Model {
public function printer() {
return $this->belongsTo(Printer::class);
}
}

Related

Access a key outside of an array and store in a variable

I have this bit of json:
[{
"id": 123,
"name": "Ice",
"categories": [{
"products": [{
"id": "66642",
"display_name": "Ice A"
},
{
"id": "62466",
"display_name": "Ice B"
},
{
"id": "62466",
"display_name": "Ice B duplicate"
}
]
}]
},
{
"id": 97,
"name": "Cakes",
"categories": [{
"products": [{
"id": "14518",
"display_name": "Cake 1"
},
{
"id": "14105",
"display_name": "Cake 2"
}
]
}]
}
]
I'm using jq 1.6 and I'm trying to get this output:
[{
"id": "62466",
"name": "Ice B",
"category": "Ice"
},
{
"id": "66642",
"name": "Ice A",
"category": "Ice"
},
{
"id": "14105",
"name": "Cake 2",
"category": "Cakes"
},
{
"id": "14518",
"name": "Cake 1",
"category": "Cakes"
}]
I've tried with this code:
.[].name as $x | map(.categories[].products[]) | unique_by(.id) | .[] |
[{
id: .id,
name: .display_name,
category: $x
}]
But I get this output:
[
{
"id": "14105",
"name": "Cake 2",
"category": "Ice"
}
]
[
{
"id": "14518",
"name": "Cake 1",
"category": "Ice"
}
]
[
{
"id": "62466",
"name": "Ice B",
"category": "Ice"
}
]
[
{
"id": "66642",
"name": "Ice A",
"category": "Ice"
}
]
[
{
"id": "14105",
"name": "Cake 2",
"category": "Cakes"
}
]
[
{
"id": "14518",
"name": "Cake 1",
"category": "Cakes"
}
]
[
{
"id": "62466",
"name": "Ice B",
"category": "Cakes"
}
]
[
{
"id": "66642",
"name": "Ice A",
"category": "Cakes"
}
]
I looks like as there are two names
Ice
Cakes
It's multiplying the output by two , one for each name instead of just returning the 4 products and I can't get the result into and array of objects. Here is a reproducible code in jqplay. Because there are some duplicate records, ie: products that have more than one category I need them to be unique. How can i fix this?
Assigning the category name in a variable and then transforming each product with unique id:
map(
.name as $category
| .categories[].products
| unique_by(.id)
| .[]
| { id, name: .display_name, $category }
)
This map produces the 4 unique items.
jq 'map(
(.categories[].products | unique_by(.id)[] | {id, name: .display_name})
+ {category: .name}
)'
[
{
"id": "62466",
"name": "Ice B",
"category": "Ice"
},
{
"id": "66642",
"name": "Ice A",
"category": "Ice"
},
{
"id": "14105",
"name": "Cake 2",
"category": "Cakes"
},
{
"id": "14518",
"name": "Cake 1",
"category": "Cakes"
}
]
Demo

TypeScript convert json structure

I cant figure out how to make this conversion iterating a json.
I have this pojo in my backend:
class Part{
Long id;
String name;
Set<Part> parts = new HashSet<>();
}
Every part can have parts and this part more parts and so on.
I get this parts from httpclient in angular and get this json:
[{
"id": 1,
"name": "Parts A and B",
"parts": [{
"id": 2,
"name": "A",
"parts": [{
"id": 4,
"name": "A1",
"parts": []
}]
},
{
"id": 3,
"name": "B",
"parts": []
}
]
},
{
"id": 2,
"name": "A",
"parts": []
},
{
"id": 3,
"name": "B",
"parts": []
},
{
"id": 4,
"name": "A1",
"parts": []
}
]
And need to convert to this to populate a PrimeNG TreeTable:
{
"data": [{
"data": {
"name": "Parts A and B",
"id": "1"
},
"children": [{
"data": {
"name": "Part A",
"id": "2"
},
"children": [{
"data": {
"name": "A1",
"id": "4"
}
}]
},
{
"data": {
"name": "Part B",
"id": "3"
},
"children": []
}
]
},
{
"data": {
"name": "Part A",
"id": "2"
},
"children": []
},
{
"data": {
"name": "Part B",
"id": "3"
},
"children": []
},
{
"data": {
"name": "A1",
"id": "4"
},
"children": []
}
]
}
How can I do that?
In angular I get this in an array parts: Part[] and need partsTree: TreeNode[]
Thanks!!!
Its just a simple conversion by a map;
interface PartAPI{
id: number;
name: string;
parts : PartAPI[];
}
interface Data {
id: number;
name: string;
}
interface Part {
data : Data;
children : Part[];
}
console.log('a')
let convert = (inputArr: PartAPI[] = []) : Part[] => {
return inputArr.map(partApi => ({ data : { id : partApi.id , name : partApi.name }, children: convert(partApi.parts) }) as Part)
}
let data : PartAPI[] = [{
"id": 1,
"name": "Parts A and B",
"parts": [{
"id": 2,
"name": "A",
"parts": [{
"id": 4,
"name": "A1",
"parts": []
}]
},
{
"id": 3,
"name": "B",
"parts": []
}
]
},
{
"id": 2,
"name": "A",
"parts": []
},
{
"id": 3,
"name": "B",
"parts": []
},
{
"id": 4,
"name": "A1",
"parts": []
}
]
console.log(convert(data));

How to set alias instead of id , i need category_id without using forloop

$categoryData = $this->compCatObj->find()->select(['CompanyCategory.id', 'CompanyCategory.name','CompanyCategory.restricted'])->contain(['CompanyItems'=>['fields'=>['CompanyItems.id','CompanyItems.company_category_id','CompanyItems.name']]])->toArray();
Response
{
"status": "success",
"message": "List of company categories",
"data": [
{
"id": 1,
"name": "Breakfast gdfgedfgdf",
"restricted": "no",
"company_items": []
},
{
"id": 2,
"name": "Breakfast",
"restricted": "yes",
"company_items": []
}
]
}
i need category_id instead of id. Is there any way to do this without using forloop.
yes you can do this by a small change in your code,
$categoryData = $this->compCatObj->find()->select(['category_id' => 'CompanyCategory.id', 'CompanyCategory.name','CompanyCategory.restricted'])->contain(['CompanyItems'=>['fields'=>['CompanyItems.id','CompanyItems.company_category_id','CompanyItems.name']]])->toArray();
Response
{
"status": "success",
"message": "List of company categories",
"data": [
{
"category_id": 1,
"name": "Breakfast gdfgedfgdf",
"restricted": "no",
"company_items": []
},
{
"category_id": 2,
"name": "Breakfast",
"restricted": "yes",
"company_items": []
}
]
}

JSON Transformation using JQ

I am trying to transform my JSON to different structure using JQ. I am able to partially achieve my new structure, however i get a additional blocks of data.
As i iterate i am able to get the structure in new format. But additional structures are coming.
Code Snippet- https://jqplay.org/s/3gulSlZiWz
JSON
{
"amazon": {
"activeitem": 2,
"createdDate": "2019-01-15T17:36:31.588Z",
"lastModifiedDate": "2019-01-15T17:36:31.588Z",
"user": "net",
"userType": "new",
"items": [
{
"id": 1,
"name": "harry potter",
"state": "sold",
"type": {
"branded": false,
"description": "artwork",
"contentLevel": "season"
}
},
{
"id": 2,
"name": "adidas shoes",
"state": "in inventory",
"type": {
"branded": false,
"description": "Spprts",
"contentLevel": "season"
}
},
{
"id": 3,
"name": "watch",
"state": "returned",
"type": {
"branded": false,
"description": "walking",
"contentLevel": "special"
}
},
{
"id": 4,
"name": "adidas shoes",
"state": "in inventory",
"type": {
"branded": false,
"description": "running",
"contentLevel": "winter"
}
}
],
"product": {
"id": 4,
"name": "adidas shoes",
"source": "dealer",
"destination": "resident"
}
}
}
JQ Query:
.amazon|
{
userType: .userType,
userName: .user,
itemCatalog: {
itemId: .items[].id,
name: .items[].name
}
}
Expected Response:
{
"userType": "new",
"userName": "net",
"itemCatalog": {
"itemId": 1,
"name": "harry potter"
},{
"itemId": 2,
"name": "adidas shoes"
}, {
"itemId": 3,
"name": "watch"
},{
"itemId": 4,
"name": "adidas shoes"
}
}
But getting something weird long duplicated response.
As already pointed out in a comment, the "expected response" is not JSON and probably not what you want anyway. The following would make sense and in any case illustrates how to iterate appropriately:
.amazon
| { userType: .userType,
userName: .user,
itemCatalog: (.items | map({ itemId: .id, name} ))
}
Output
{
"userType": "new",
"userName": "net",
"itemCatalog": [
{
"itemId": 1,
"name": "harry potter"
},
{
"itemId": 2,
"name": "adidas shoes"
},
{
"itemId": 3,
"name": "watch"
},
{
"itemId": 4,
"name": "adidas shoes"
}
]
}

How to use jq to provide ungrouped result (i.e. parent and children attributes separately)?

There is a JSON which I am getting with a script:
{
"data": [
{
"type": "physical",
"children": [
{
"children": [
{
"type": "CISCO",
"description": "test",
"id": 53,
"min_max_policy": {
"legal_hold": 0,
"name": "child 1",
"max": 60,
"min": 0
},
"name": "child 1",
"parent_id": 1,
"parent_name": "parent 1",
"system_id": 1,
"system_name": "cisco 1",
"version": "None"
},
{
"type": "CISCO",
"description": "test",
"id": 54,
"min_max_policy": {
"legal_hold": 0,
"name": "child 2",
"max": 60,
"min": 0
},
"name": "child 2",
"parent_id": 1,
"parent_name": "parent 1",
"system_id": 2,
"system_name": "cisco 1",
"version": "None"
}
],
"type": "network devices"
}
],
"machine_type": "X32",
"min_max_policy": {
"id": 1,
"name": "parent 1",
"max": 60,
"min": 0
},
"name": "parent 1",
"system_id": 1,
"system_name": "sys 1 name",
"version": "1.0"
},
{
"type": "physical",
"children": [
{
"children": [
{
"type": "HP",
"description": "test",
"id": 55,
"min_max_policy": {
"legal_hold": 0,
"name": "child 3",
"max": 60,
"min": 0
},
"name": "child 3",
"parent_id": 2,
"parent_name": "parent 2",
"system_id": 3,
"system_name": "hp 1",
"version": "None"
}
],
"type": "network devices"
}
],
"machine_type": "X32",
"min_max_policy": {
"id": 2,
"legal_hold": 0,
"name": "parent 2",
"max": 60,
"min": 0
},
"name": "parent 2",
"system_id": 2,
"system_name": "sys 2 name",
"version": "1.0"
},
{
"type": "physical",
"machine_type": "X32",
"min_max_policy": {
"id": 3,
"name": "parent 3",
"max": 60,
"min": 0
},
"name": "parent 3",
"system_id": 3,
"system_name": "sys 3 name",
"version": "1.0"
}
]
}
It has 3 parent objects and children. For example, "parent 1" has 2 children, "parent 2" has 1 and "parent 3" has 0.
How to format this JSON with jq to display it in such way:
{
"name": "child 1",
"parent_name": "parent 1",
"system_name": "cisco 1"
}
{
"name": "child 2",
"parent_name": "parent 1",
"system_name": "cisco 1"
}
{
"name": "parent 1",
"parent_name": null,
"system_name": "sys 1 name"
}
{
"name": "child 3",
"parent_name": "parent 2",
"system_name": "hp 1"
}
{
"name": "parent 2",
"parent_name": null,
"system_name": "sys 2 name"
}
{
"name": "parent 3",
"parent_name": null,
"system_name": "sys 3 name"
}
?
I have tried some options but jq is grouping parent and children. I can't find a way how to display it in the mentioned way.
Except for the ordering, the following jq program produces the desired output, as shown below:
..
| select(.name? and .system_name?)
| {name, parent_name, system_name}
Transcript
$ jq -f program.jq input.json
{
"name": "parent 1",
"parent_name": null,
"system_name": "sys 1 name"
}
{
"name": "child 1",
"parent_name": "parent 1",
"system_name": "cisco 1"
}
{
"name": "child 2",
"parent_name": "parent 1",
"system_name": "cisco 1"
}
{
"name": "parent 2",
"parent_name": null,
"system_name": "sys 2 name"
}
{
"name": "child 3",
"parent_name": "parent 2",
"system_name": "hp 1"
}
{
"name": "parent 3",
"parent_name": null,
"system_name": "sys 3 name"
}