Laravel Order Address Relationship to Array - php

Hello I have a Query to create a PDF with that.
$data = Order::with('invoice')->with('delivery')->with('item')->find($id)->toArray();
That works fine. But The 'item' has a relationship to the Product Model.
How can i get the data of this product also in this query?
The current array is that.
0 => array:8 [▼
"id" => 11
"order_id" => 38
"product_id" => 2
"qty" => 999999
"total" => "99999.99"
"paid" => 0
"created_at" => "2015-11-16 11:21:40"
"updated_at" => "2015-11-16 12:35:33"
]

So to close this question, you can eager load relations of relations using the dot notation:
->with('item.product')

Related

How can I make a query with the 3 tables, adding the total of invoices and tickets in laravel

I have created a query so that it returns me the total of invoices and tickets, but when using joinleft I see that they do not return all the clients. Those who do not have tickets, for example, do not return them.
$clients = Client::select('clients.id','clients.name','clients.nif', \DB::raw("SUM(invoices.total) as total1, SUM(tickets.total) as total2, (SUM(invoices.total) + SUM(tickets.total)) as total"))
->leftJoin('invoices', function ($join1) use($invoice_year){
$join1->on('clients.id', '=', 'invoices.client_id')
->whereYear('invoices.invoice_date','=', $invoice_year)
->groupBy('clients.id','clients.name', 'clients.nif');
})
->leftJoin('tickets', function ($join2) use($invoice_year){
$join2->on('clients.id', '=', 'tickets.client_id')
->whereYear('tickets.created_at','=', $invoice_year)
->groupBy('clients.id','clients.name', 'clients.nif');
})
->groupBy('clients.id','clients.name', 'clients.nif')
->having('total','>',3005)
->get();
if I do a dd, it shows this:
array:3 [▼
0 => array:6 [▼
"id" => 19
"name" => "gdfgdf"
"nif" => "gdrfgf"
"total1" => "40342.08"
"total2" => "29190.40"
"total" => "69532.48"
]
1 => array:6 [▼
"id" => 818
"name" => "gfgf"
"nif" => "hhfghgf"
"total1" => "5460.62"
"total2" => "18138.00"
"total" => "23598.62"
]
2 => array:6 [▼
"id" => 2836
"name" => "Testing"
"nif" => "gfhgh"
"total1" => "3026.78"
"total2" => "7735.00"
"total" => "10761.78"
]
]
The ones he returns are fine, but there are more customers. I think in total the query should return about 4 or 5 customers. How can I make a query with the 3 tables, adding the total of invoices and tickets? even if there is no record of one table or another. Sorry for my english. Thank you.
You should make a full join instead of a left join.
The left join will only merge tables who has a result on all 3 tables.
The full join will merge your tables whatever the result.

lumen/laravel pluck not working in with relationship

I have pivot table vacancy_tag. I tried different options (value,select, lists and etc), but didn't go further than that. My request:
Vacancy::where('id',1)->with(['tags' => function ($q){
$q->select('tags.id')->pluck('id');
}])->get()->toArray();
return this:
"id" => 1,
"title" => "pm",
"tags" => array:2 [▼
0 => array:1 [▼
"id" => 1
]
1 => array:1 [▼
"id" => 2
]
but need this:
"id" => 1,
"title" => "pm",
"tags" => [
1,
2
]
I would like to receive the data result in a query, without using a methods transformation of the collection, if possible.
PS: I slightly corrected the example, need to display all fields of the vacancy + an array of tags.

Convert Collection to Array with I=ID in Laravel

I am beginner php developer.
I have small problem with my array. I use in my project Laravel 7
I have this code:
$items = collect($discount->products->toArray());
It's result me:
Illuminate\Support\Collection {#1783 ▼
#items: array:2 [▼
0 => array:17 [▼
"id" => 1
"product_category_id" => 5
"image_id" => null
"vat_type_id" => 1
"name" => "Produkt 1"
"slug" => "produkt-1"
"lead" => "<p>fewferfer</p>"
"description" => "<p> </p>"
"price" => "123.00"
"loyalty_program_points_price" => 2
"min_student_level" => null
"marked_as_available_at" => "2020-09-30T11:45:51.000000Z"
"deactivated_at" => null
"created_at" => "2020-09-30T11:45:23.000000Z"
"updated_at" => "2020-09-30T11:45:51.000000Z"
"deleted_at" => null
"pivot" => array:3 [▶]
]
1 => array:17 [▼
"id" => 2
"product_category_id" => 5
"image_id" => null
"vat_type_id" => 1
"name" => "Produkt 2"
"slug" => "produkt-2"
"lead" => "<p> </p>"
"description" => "<p>fergfer</p>"
"price" => "21.00"
"loyalty_program_points_price" => 3
"min_student_level" => null
"marked_as_available_at" => "2020-09-30T11:45:38.000000Z"
"deactivated_at" => null
"created_at" => "2020-09-30T11:45:38.000000Z"
"updated_at" => "2020-09-30T11:45:38.000000Z"
"deleted_at" => null
"pivot" => array:3 [▶]
]
]
}
I need convert it to this result:
Array{
1 => 1 (id)
2 => (id)
}
How can I make it?
I need array with only id values
Collections have the pluck method, which allows you to grab all the values of a specific key. To get all of the ids, you'd just need to do
$ids = $items->pluck('id')->all();
If you want them to have the same key as the id as well, pass that as the second parameter
$ids = $items->pluck('id', 'id')->all();
As a note, if products is a relation, then it's already a collection. You do not need to convert it to an array, then back to a collection:
$product_ids = $discount->products->pluck('id')->all();

Can't json_encode() an array or Laravel collection: "Type is not supported"

I don't know what I'm doing wrong since it works with all the other models in the app. I refreshed and reseeded the database multiple times. The models extend the same abstract methods.
This is the code in the controller:
$substrates = $this->substrates->all()->toArray();
$temp = json_encode($substrates);
dd($temp, json_last_error(), json_last_error_msg(), $substrates);
This is the dd() output:
false
8
"Type is not supported"
array:119 [▼
0 => array:21 [▼
"id" => 1
"name" => "Wood Free"
"machine_id" => 2
"classification" => "Cover"
"origins" => "Coming Soon"
"duplex" => true
"color" => "Translucents"
"texture" => "Leather"
"finish" => "Felt"
"product_type" => "Sheet"
"caliper" => "0.06"
"m_weight" => 70
"width" => "46.40"
"height" => "32.00"
"pic" => stream resource #17 ▶}
"price" => "0.30"
"created_by" => 38
"updated_by" => 16
"deleted_at" => null
"created_at" => "2018-01-27 08:00:11"
"updated_at" => "2018-01-27 08:00:11"
]
1 => array:21 [▶] ....
When I use JSON_PARTIAL_OUTPUT_ON_ERROR I get a json string.
The reason for the error is the fact, that you're storing a stream resource in pic field of the serialised object that can't be serialised to JSON.
You can tell Eloquent model to skip selected attributes when they're converted to an array by setting a $hidden attribute in your model:
class Substrate extends Model {
protected $hidden = ['pic'];
}

How can I select certain columns from my recursive child call

I'm pretty new to Eloquent and I'm having issues wrapping my head around something.
Basically I have a table which I'm recursively grabbing children from within the same table.
public function children() {
return $this->hasMany(static::class, 'parent_org_id');
}
public function childrenRec()
{
return $this->children()->with('childrenRec');
}
Where childrenRec is a recursive call to all children based on 'parent_org_id'
I'm calling it from the following in a static function, as of right now I only want the id and the name_en of the org
self::select('id','name_en')->where('parent_org_id','=',0)->with('childrenRec')->get()->toArray();
which is grabbing the top level org (my top level org has a parent_org_id of 0).
My issue is that in the recursively grabbed children it doesn't limit it to the id and the name_en
My question boils down to:
How can I select only certain columns from my recursive child calls, as well is this the 'proper' way of doing things?
My returned array looks like this.
array:1 [▼
0 => array:4 [▼
"id" => 1
"name_en" => "Org Unit"
"org_type" => null
"children_rec" => array:2 [▼
0 => array:27 [▼
"id" => 2
"name_en" => "My First Orgunit."
"code" => null
"abbreviation" => null
"address1" => "222 Street Street"
"address2" => null
"city_id" => 1
"province_id" => 14
"postalcode" => "C161L7"
"country_id" => 38
"contact_name" => null
"contact_title" => null
"email" => "test#test.com"
"fax" => "902-555-5555"
"phone1" => "5125125125125"
"phone2" => null
"org_type_id" => 1
"parent_org_id" => 1
"ref_id" => 79
"has_users" => 1
"created_at" => "2016-11-02 18:47:55"
"updated_at" => "2016-11-02 18:47:55"
"org_type" => array:4 [▶]
"children_rec" => array:1 [▶]
]
1 => array:27 [▶]
]
]
]
Thanks in advance.
To access the relation query in the with() method you use an array with the name of the relationship as the key and a closure with an instance of QueryBuilder injected.
One 'gotcha' that took me forever to track down a solution to when doing this is, your parent and children queries need to include the key that associates their relationship because the models are attached/associated with each other after both queries are run separately. Those models are associated with each other using the columns defined in the relation on the model. Without the columns used in the association of the models in your query, the related models won't be attached. In your situation it would be:
self::select('id','name_en')
->where('parent_org_id','=',0)
->with(['childrenRec' => function($query) {
return $query->select('id', 'name_en', 'parent_org_id');
}])
->get()
->toArray();
If you don't include the parent_org_id in the subquery the relationships won't get attached.
Docs
Try this:
public function childrenRec()
{
return $this->children()->with(['childrenRec' => function($query){
$query->select('id','name_en');
}]);
}

Categories