Property [id] does not exist on this collection instance in laravel - php

store a query in array and call the result in view page show Property [id] does not exist on this collection
in controller
foreach($carvalue as $row){
$products[]=DB::table('products')->where('id',$row['product_id'] )->get();
}
and pass the products variable to view page and print the value like
foreach($products as $prod){
if($prod->id==$rows['product_id']){
//code
}
}
show the error and i dd($produts) the result is
array:2 [▼
0 => Illuminate\Support\Collection {#491 ▼
#items: array:1 [▼
0 => {#503 ▼
+"id": "130"
+"title": "Rfdfd"
+"sku": "vbff"
+"sub_title": "RC10 Matte Graphite"
}
]
}
1 => Illuminate\Support\Collection {#505 ▶}
]
why show this type error and how to solve it ?

In your query, replace get() with first()
$products[]=DB::table('products')->where('id',$row['product_id'] )->first();
OR
reference the first (and only) $products as $products[0], so it gets the id from the FIRST (and only, in your case) product
foreach($products[0] as $prod){
if($prod->id==$rows['product_id']){
//code
}
}

Related

How can I sort chosen collection in multidimensional array in laravel php

i want to sort by cu_customer_shortname in nested array collection but seems i cant see any ways do it . here is my example of my array list
{#1811 ▼
+"co_group_id": "ADUKO"
+"co_description": "ADUKO"
+"co_customer_id": "038"
+"co_active_sts": "1"
+"irel__h_d_rel_group_customer": array:3 [▼
0 => {#1812 ▼
+"ID": "5"
+"gc_group_id": "ADUKO"
+"gc_customer_id": "038"
+"irel__h_d_customer_master": {#1813 ▶}
}
1 => {#1814 ▼
+"ID": "6"
+"gc_group_id": "ADUKO"
+"gc_customer_id": "044"
+"irel__h_d_customer_master": {#1815 ▼
+"cu_customer_ID": "044"
+"cu_customer_Shortname": "JPEN" <------ Sort this shortname
}
}
]
Here is my code that i already tried
$groupList = $contents->data->groupList;
$order = $groupList->irel__h_d_rel_group_customer; //arrayList
$collection = new \Illuminate\Support\Collection($groupList); //collection
$sorted = $collection->SortByDesc('irel__h_d_customer_master.cu_customer_Shortname'); // sorted by cu_customer_shortname
My code not return any error but my list remain and not sorted by cu_customer_Shortname

Get the limited items of each type grouped items laravel

I have a table called books and another table called categories and it has item_id, type , and others .. columns in categories and type in enum (1/2/3)
Now I am trying to get the limited items of each group like 3 items from each type
So for that I am trying to get the 9 categores 3 each which has different type
Like This
Illuminate\Database\Eloquent\Collection {#2067 ▼
#items: array:3 [▼
1 => Illuminate\Database\Eloquent\Collection {#2060 ▼
#items: array:3 [▶]
}
2 => Illuminate\Database\Eloquent\Collection {#2055 ▼
#items: array:3 [▶]
}
3 => Illuminate\Database\Eloquent\Collection {#2026 ▼
#items: array:3 [▶]
}
]
}
So far I have tried this with .
$categoryLists = Category::where('display_at_home', 1)->limit(3)->get()->grouBy('type');
But it is not doing the work
You can use the Eloquent Eager Limit package as follows.
class Book extends Model
{
use \Staudenmeir\EloquentEagerLimit\HasEagerLimit;
...
}
class Category extends Model
{
use \Staudenmeir\EloquentEagerLimit\HasEagerLimit;
...
}
Then to get the relation you desire, you can do:
$categoryLists = Category::where('display_at_home', 1)
->limit(3)
->with(['books' => function($query) {
$query->limit('3');
}])
->get();

how to sum a collection after groupBy laravel

i have a collection named detailed as below :
Collection {#1421 ▼
#items: array:2 [▼
3943 => Collection {#1419 ▼
#items: array:2 [▼
0 => RoomPricingHistory {#923 ▶}
1 => RoomPricingHistory {#1042 ▶}
]
}
3944 => Collection {#1420 ▼
#items: array:2 [▼
0 => RoomPricingHistory {#1153 ▶}
1 => RoomPricingHistory {#1264 ▶}
]
}
]
}
now i want to get the sum of RoomPricingHistory for 3943 item and 3944 ofc it can be more of 2 item so i want to get the sum of each collection how can i achieve that ??
The Collection sum method can take a callback so you can define what is going to be calculated. In this case you can call sum on the main collection and in the callback which will give you access to the internal collections, also call sum.
$detailed->sum(function ($group) {
return $group->sum('sales_price');
});
Laravel 6.x Docs - Collections - Available Methods - sum
Since that isn't what you are looking for, you can use something like mapWithKeys to go through the collection and call sum for the groups to get the sum for each group:
$sums = $detailed->mapWithKeys(function ($group, $key) {
return [$key => $group->sum('sales_price')];
});
Laravel 6.x Docs - Collections - Available Methods - mapWithKeys

Laravel collection: flatten method ignored

Possibly an obvious one, but here it is:
After dd'ing a query result, I have:
$items = DB::table('my_table')->get(['id']);
dd($items);
Which results in:
Collection {#228 ▼
#items: array:1 [▼
0 => {#227 ▼
+"id": 2
}
]
}
Then, when I try to flatten it, it ignores me:
dd($items->flatten());
Resulting in:
Collection {#209 ▼
#items: array:1 [▼
0 => {#227 ▼
+"id": 2
}
]
}
Shouldn't I receive something like the flattened version of the collection?
How can I do it?
Thanks in advance.
You need to check it like
$flattened = $items->flatten();
dd($flattened->all());
// or dd($items->flatten()->all());
Source from official documentation.
If you want to fetch id and completely flatten then use
$items = DB::table('my_table')->pluck('id');
dd($items);
Here is the pluck link.

Merge 2 Collections or Arrays by Key

I'm trying my best to prevent from looping over a collection to get more data that I need for a data table. I need a way to combine these 2 collections or arrays.
I have 2 functions inside a model that calls an API that gives me an array of data that I convert into a collection.
Orders()
public function orders(){
$orders_array = $this->api()->request('orders.json');
return collect($orders_array);
}
Returns:
Collection {#274 ▼
#items: array:2 [▼
0 => {#282 ▼
+"id": 628602961977
}
1 => {#270 ▶}
order_meta()
public function order_meta($order_id){
$order_metafields_array = $this->api()->request('meta.json');
return collect($order_metafields_array);
}
Returns:
Collection
#items: [
+"name": "meta_data"
]
What I am wanting to do is inject the order_meta() into each order using the order ID.
I guess I am wanting to do something like this:
public function orders_detailed(){
$orders = $this->orders();
$keyed = $orders->mapWithKeys(function ($item) {
return [$item['meta'] => $this->order_metafields($item['id'])];
});
}
In hopes it will return:
Collection {#274 ▼
#items: array:2 [▼
0 => {#282 ▼
+"id": 628602961977
+meta => {
+"name":"meta_data"
}
}
1 => {#270 ▶}
Is something like this possible? I'm also open to merging it in as an array first, then creating a collection.

Categories