I am passing below data through API in postman.
I want to access these values from collection inside array for Items.
{
"user":"abc",
"supplier":"xyz",
"pdate":"1",
"items":[
{
"product":"Apple",
"qty":"1",
"rate":"40",
"amount":"40"
},
{
"product":"Banana",
"qty":"6",
"rate":"4",
"amount":"24"
}
]
}
Do you mean that you want to conver the items into Collection? If that's the case, you can do:
$data = json_decode('{ "user":"abc", "supplier":"xyz", "pdate":"1", "items":[ { "product":"Apple", "qty":"1", "rate":"40", "amount":"40" }, { "product":"Banana", "qty":"6", "rate":"4", "amount":"24" } ] }', true, 512, JSON_THROW_ON_ERROR);
$items = collect($data['items']);
dd($items);
This will output:
Illuminate\Support\Collection {#1545
#items: array:2 [
0 => array:4 [
"product" => "Apple"
"qty" => "1"
"rate" => "40"
"amount" => "40"
]
1 => array:4 [
"product" => "Banana"
"qty" => "6"
"rate" => "4"
"amount" => "24"
]
]
}
Please note, I have just added and json_decoded the data, for the sake of the example, but just do this in your controller where the data is passed in.
Related
Is it possible to use $request->except on a nested array? This is the request data:
[▼
"id" => 1
"products" => array:1 [▼
0 => array:4 [▼
"id" => 1
"name" => "sample product"
"date" => "07/04/2022"
"frequency" => array:1 [ …1]
]
]
]
My goal is to remove some key value pairs e.g. removing id, date and frequency. My desired result would be:
[▼
"id" => 1
"products" => array:1 [▼
0 => array:1 [▼
"name" => "sample product"
]
]
]
What I've tried so far is to use Arr:except function:
$request->products = Arr::except($request->products['0'], ['id', 'date', 'frequency']);
But this should be applied on all items. Let's say I have two item products, the desired result would be:
[▼
"id" => 1
"products" => array:2 [▼
0 => array:1 [▼
"name" => "sample product"
]
1 => array:1 [▼
"name" => "sample product 2"
]
]
]
Need your inputs on what's the best approach for this. Thank you!
Unless I am missing something obvious, could you not just iterate over the products in your $request object?
$filtered = [];
foreach ($request->products as $product) {
$filtered[] = Arr::except($product, ['id', 'date', 'frequency']);
}
dd($filtered);
Resulting in $filtered containing:
^ array:2 [▼
"id" => 1
"products" => array:2 [▼
0 => array:1 [▼
"name" => "sample product"
]
1 => array:1 [▼
"name" => "sample product 2"
]
]
]
Update 1
I can see that the $request except only works on the top level
Not sure what you mean by this as the above principle works for me.
$filtered = [];
foreach ($request->products as $product) {
$filtered[] = Arr::except($product, ['id', 'date', 'frequency']);
}
$request->merge(['products' => $filtered]);
If I supply the above with the following:
{
"id": 1,
"products": [
{
"id": 1,
"name": "sample product",
"date": "07/04/2022",
"frequency": []
},
{
"id": 2,
"name": "sample product 2",
"date": "07/04/2022",
"frequency": []
}
]
}
Then do a dd($request->products); I get:
^ array:2 [▼
0 => array:1 [▼
"name" => "sample product"
]
1 => array:1 [▼
"name" => "sample product 2"
]
]
I have a nested collection that I want to transform, pulling some keys "up a level" and discarding some other keys.
Every item in the collection has an allergens property.
"allergens": [
{
"id": 2001,
"info": "Allergy advice",
"status": "does_contain",
"product_id": 71576,
"allergen_id": 1,
"allergen": {
"id": 1,
"name": "Celery"
}
},
{
"id": 2002,
"info": "Allergy advice",
"status": "may_contain",
"product_id": 71576,
"allergen_id": 11,
"allergen": {
"id": 11,
"name": "Peanuts"
}
}
],
I need to make each items allergens property, look like
"allergens": [
{
"id": 1,
"name": "Celery"
"status": "does_contain",
},
{
"id": 11,
"name": "Peanuts"
"status": "does_contain",
},
],
I've tried the following:
$collection = $collection->transform(function ($item, $key) {
$item->allergens = $item->allergens->map(function ($allergen) {
return [
'id' => $allergen->allergen->id,
'name' => $allergen->allergen->name,
'status' => $allergen->status,
];
});
return $item;
});
But it doesn't overwrite the allergens property
Since you're posting your collection as JSON, I reverse engineered what your actual collection would look like. Turns out, your transform() works fine as far as I can tell. Maybe that helps you to find differences between my and your collection which might lead you to your problem/solution:
$collection = collect([
(object)[
"allergens" => collect([
(object)[
"id" => 2001,
"info" => "Allergy advice",
"status" => "does_contain",
"product_id" => 71576,
"allergen_id" => 1,
"allergen" => (object)[
"id" => 1,
"name" => "Celery"
]
],
(object)[
"id" => 2002,
"info" => "Allergy advice",
"status" => "may_contain",
"product_id" => 71576,
"allergen_id" => 11,
"allergen" => (object)[
"id" => 11,
"name" => "Peanuts"
]
]
]),
]
]);
$collection = $collection->transform(function ($item, $key) {
$item->allergens = $item->allergens->map(function ($allergen) {
return [
'id' => $allergen->allergen->id,
'name' => $allergen->allergen->name,
'status' => $allergen->status,
];
});
return $item;
});
dd($collection);
Result:
Illuminate\Support\Collection {#1779 ▼
#items: array:1 [▼
0 => {#1791 ▼
+"allergens": Illuminate\Support\Collection {#1775 ▼
#items: array:2 [▼
0 => array:3 [▼
"id" => 1
"name" => "Celery"
"status" => "does_contain"
]
1 => array:3 [▼
"id" => 11
"name" => "Peanuts"
"status" => "may_contain"
]
]
}
}
]
}
This question already has answers here:
Sorting Laravel Collection via Array of ID's
(3 answers)
Closed last year.
Suppose I have a collection like this named categories :
Collection {#447
#items: array:3 [
0 => array:6 [
"pos" => "0"
"col" => "1"
"row" => "1"
"size_x" => "2"
"size_y" => "1"
"cat_id" => "1"
]
1 => array:6 [
"pos" => "0"
"col" => "3"
"row" => "1"
"size_x" => "1"
"size_y" => "1"
"cat_id" => "11"
]
2 => array:6 [
"pos" => "0"
"col" => "1"
"row" => "2"
"size_x" => "2"
"size_y" => "1"
"cat_id" => "10"
]
]
}
On the other hand there is an array of IDs like this :
[11,10,1]
Now I want to sort element of that collection based on their cat_id index as sorted in an array. means I want result array to be like this :
Collection {#447
#items: array:3 [
0 => array:6 [
"pos" => "0"
"col" => "3"
"row" => "1"
"size_x" => "1"
"size_y" => "1"
"cat_id" => "11"
]
1 => array:6 [
"pos" => "0"
"col" => "1"
"row" => "2"
"size_x" => "2"
"size_y" => "1"
"cat_id" => "10"
]
2 =>array:6 [
"pos" => "0"
"col" => "1"
"row" => "1"
"size_x" => "2"
"size_y" => "1"
"cat_id" => "1"
]
]
}
I used below codes but does not work properly:
$grids_arr = [11,10,1];
$categories = $categories->sortBy(function ($model) use ($grids_arr) {
return array_search($model->cat_id, $grids_arr);
});
Update :
This is my completed Code I'm using:
$categories = Category::isTile()->get();
$grids = collect(config('settings.data_grids'));// This is an array of IDs
if ($grids->isNotEmpty()) {
$grids_arr = $grids->pluck("cat_id")->toArray();
$grids_arr = array_map('intval', $grids_arr); //convert string array elements to integer
$categories = $categories->sortBy(function ($catg) use ($grids_arr) {
return array_search($catg->cat_id, $grids_arr);
});
}
You probably have not responded because of differences between config('settings.data_grids') And $categories. So edit your code as follows
Please add these three lines to your code:
$catIds = $categories->pluck('cat_id')->toArray(); //get all cat_id
$diff = array_diff($catIds, $grids_arr); // difference array from query
$grids_arr = array_merge($grids_arr , $diff); //merge difference with array
for example:
$categories = Category::select('*')->get(); //for example
$array = [
[
"pos" => "0",
"col" => "1",
"row" => "1",
"size_x" => "2",
"size_y" => "1",
"cat_id" => 1,
],
[
"pos" => "0",
"col" => "1",
"row" => "2",
"size_x" => "2",
"size_y" => "1",
"cat_id" => 10,
]
];
$grids = collect($array);
if ($grids->isNotEmpty()) {
$grids_arr = $grids->pluck("cat_id")->toArray();
$grids_arr = array_map('intval', $grids_arr);
//Please add these three lines to your code
$catIds = $categories->pluck('cat_id')->toArray();
$diff = array_diff($catIds, $grids_arr);
$grids_arr = array_merge($grids_arr, $diff);
//-----
$sorted = $categories->sortBy(function ($model) use ($grids_arr) {
return array_search($model->cat_id, $grids_arr);
});
return $sorted->values()->all();
}
please check the code below.
$array = [
0 => [
"pos" => "0",
"col" => "3",
"row" => "1",
"size_x" => "1",
"size_y" => "1",
"cat_id" => "11",
],
1 => [
"pos" => "0",
"col" => "1",
"row" => "2",
"size_x" => "2",
"size_y" => "1",
"cat_id" => "10",
],
2 => [
"pos" => "0",
"col" => "1",
"row" => "1",
"size_x" => "2",
"size_y" => "1",
"cat_id" => "1",
],
];
$array = array_reverse(array_sort($array, function ($value)
{
return $value['cat_id'];
}));
dd($array);
Hope that it helps you.
Suppose I have a a collection like this :
Collection {#447
#items: array:3 [
0 => array:6 [
"pos" => "0"
"col" => "1"
"row" => "1"
"size_x" => "2"
"size_y" => "1"
"cat_id" => "1"
]
1 => array:6 [
"pos" => "0"
"col" => "3"
"row" => "1"
"size_x" => "1"
"size_y" => "1"
"cat_id" => "11"
]
2 => array:6 [
"pos" => "0"
"col" => "1"
"row" => "2"
"size_x" => "2"
"size_y" => "1"
"cat_id" => "10"
]
]
}
Now I want an array like this that get only cat_id indexes.
[1,11,10]
How can I do that in laravel ?
There are a few ways you can do that. The easiest is to use the pluck method:
$collection->pluck('cat_id');
This will return a new Collection. If you want to convert it to an array just use toArray() or all() afterwards:
$items = $collection->pluck('cat_id')->toArray();
$collection->pluck("cat_id")->toArray();
Use pluck method and pass field name as an argument:
$new_collection = $your_collection->pluck('cat_id');
I'm im using zendframework/zend-diactoros with Laravel. I have a problem when I sent json data like
{
"data": {
"value": ""
}
}
Then, in controller
// #var $request Psr\Http\Message\ServerRequestInterface
var_dump($request->getParsedBody())
/*
array:1 [
"data" => array:1 [
"value" => null // I expect "value" => ""
]
]
More examples:
I sent
{
"data": {
"value": null
}
}
var_dump
var_dump($request->getParsedBody())
/*
array:1 [
"data" => array:1 [
"value" => null // that's OK
]
]
I sent
{
"data": {
"value": "x"
}
}
var_dump
var_dump($request->getParsedBody())
/*
array:1 [
"data" => array:1 [
"value" => "x" // that's OK
]
]
Only first example has a problem, but I dont have any idea if this is correct. Maybe exists a way to dump exact received data with this PSR7 library.