I have a search result from image model and $photo saved the data which $photo->type == 'photo'.
$photo = $image->filter(function($photo,$key) use($path){
if($photo->type == 'photo'){
$photo->url = $path.$photo->image;
return $photo;
}
});
Here is the $photo collection and is there any way to array_values() the items data?
Collection {#352 ▼
#items: array:3 [▼
2 => ImageBanquet {#349 ▶}
3 => ImageBanquet {#350 ▶}
4 => ImageBanquet {#351 ▶}
]
}
Check values() collection helper.
$values = $collection->values();
Related
I have data I return them into collection but it does not return as i wish and i need help to fix it.
output
array:11 [▼
0 => Collection {#2762 ▼
#items: array:3 [▼
0 => Product {#2758 ▶}
1 => Product {#2759 ▶}
2 => Product {#2760 ▶}
]
}
1 => Collection {#2792 ▼
#items: []
}
2 => Collection {#2948 ▼
#items: []
}
3 => Collection {#3102 ▼
#items: []
}
4 => Collection {#3216 ▼
#items: []
}
5 => Collection {#2886 ▼
#items: array:10 [▶]
}
6 => Collection {#2920 ▼
#items: array:3 [▶]
}
7 => Collection {#3046 ▼
#items: array:12 [▶]
}
8 => Collection {#3074 ▼
#items: []
}
9 => Collection {#3188 ▼
#items: array:7 [▶]
}
10 => Collection {#3288 ▼
#items: []
}
]
Code
$category = Category::where('slug','=',$catslug)->with('childs', 'parent')->first();
$pp1[] = Product::where('category_id',$category->id)->get();
foreach($category->childs as $first){
$pp2[] = Product::where('category_id',$first->id)->get();
if(count($first->childs)> 0){
foreach($first->childs as $second){
$pp3[] = Product::where('category_id',$second->id)->get();
}
}
}
$products = array_merge($pp1,$pp2,$pp3);
dd($products);
What it should return
It should return all products from all arrays in 1 array only and not categorized by Collection {#2792 ▼ I need to get only collections with items and not the ones that are empty.
Any idea?
update
I've also tried this:
$pp1 = [];
$pp2 = [];
$pp3 = [];
$pp1 = Product::where('category_id',$category->id)->get();
foreach($category->childs as $first){
$pp2 = Product::where('category_id',$first->id)->get();
if(count($first->childs)> 0){
foreach($first->childs as $second){
$pp3 = Product::where('category_id',$second->id)->get();
}
}
}
it returns:
array_merge(): Argument #1 is not an array
You can get all products of the category, of its children and of its grand-children as,
$category = Category::where('slug','=',$catslug)
->with([
'products',
'childs.products',
'childs.childs.products',
'parent'
])
->first()
->toArray();
$products = array_merge([
data_get($category, 'products'),
array_collapse(data_get($category, 'childs.*.products')),
array_collapse(data_get($category, 'childs.*.childs.*.products'))
]);
Fiddle : https://implode.io/ebXrD8
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.
I need to import excel data in db. but is giving me tough time.
here is my controller function
$path = Input::file('import_file')->getRealPath();
$data = \Excel::load($path)->get();
if($data->count()){
foreach ($data as $key => $value) {
$arr[] = ['product_id' => $value->price, 'details' =>
$value->stock];
}
if(!empty($arr)){
\DB::table('stock')->insert($arr);
dd('Insert Record successfully.');
}
}
where price and stock is fields in excel.
below is the error
Property [price] does not exist on this collection instance.
here is dd() of my controller function when i am trying to save excel data in db.
SheetCollection {#375 ▼
#title: ""
#items: array:3 [▼
0 => RowCollection {#498 ▼
#heading: array:2 [▼
0 => "stock"
1 => "price"
]
#title: "Sheet1"
#items: array:3 [▼
0 => CellCollection {#462 ▼
#title: null
#items: array:2 [▼
"stock" => 2.0
"price" => 3.0
]
}
1 => CellCollection {#481 ▼
#title: null
#items: array:2 [▼
"stock" => 3.0
"price" => 2.0
]
}
2 => CellCollection {#480 ▼
#title: null
#items: array:2 [▶]
}
]
}
1 => RowCollection {#533 ▶}
I'll paste my import function here, modify them for your needs.
My excel template has only 1 sheet and the first row contains the field names.
// Retrieve file
$file = $request->file('file');
$ext = $file->getClientOriginalExtension();
// Just my helper method to create filenames
$filename = Helper::create_filename($ext);
// Move file
$path ="uploads/import";
$file->move($path, $filename);
Excel::selectSheetsByIndex(0)->load("{path}/{$filename}", function($reader) {
$sheet = $reader->all();
foreach ($sheet as $i => $row) {
$data = $row->toArray(); // Utilize this data to fill your model
}
});
CHANGE EXCEL SHEET COLUMN NAME LIKE THAT
price stock
100 400
200 15
12 167
I have an array in laravel named $destinations and the values are:
Collection {#391 ▼
#items: array:2 [▼
"Chicago, Illinois" => Collection {#398 ▼
#items: array:10 [▶]
}
"New York City, New York" => Collection {#403 ▼
#items: array:10 [▶]
}
]
}
This array is grouped by city name just like Chicago and New York, now I want to add one array item after the city name, it should be like this:
Collection {#391 ▼
#items: array:2 [▼
"Chicago, Illinois" => Collection {#398 ▼
#items: array:10 [▶]
"days" => "2"
}
"New York City, New York" => Collection {#403 ▼
#items: array:10 [▶]
"days" => "7"
}
]
}
I'm trying to use array_merge when i loop the array, I use foreach($destinations as $key => $destination) in the looping, basically the $key will return increment number like 0, 1, 2, etc but in this case it return the city name. Anyone knows how to do it? or how to make the $key still increment number after grouped by in laravel?
As I see your $destinations variable is a collection. So you can iterate through it using each method.
for example:
$destinations = $destinations->each(function ($item, $key) {
// You can modify $item here
// $item['days'] = <no_of_days>;
});
To get the increment number, you can do something like this:
$element_position = 0;
$destinations = $destinations->each(function ($item, $key) use ($element_position) {
// You can modify $item here
// $item['days'] = <no_of_days>;
// You can get the $element_position here
// Iterate $element_position
$element_position++;
});
I have this collection:
Collection {#604 ▼
#items: array:4 [▼
0 => CarsMark {#596 ▶}
1 => CarsMark {#594 ▶}
2 => CarsMark {#594 ▶}
3 => CarsMark {#595 ▶}
]
}
As you can see there is two the same items (594). How can i retrieve all items from with collection without the same items?
Use the collection's unique method:
$unique = $collection->unique();