ErrorException in mycontroller on line 137 Undefined offset: 2 - laravel-5

I am getting this exception: Undefined offset: 2 when inserting a multidimensional array in the database. This is my loop at which the error is pointing at:
$order_details = [];
for($i= 0; $i < count($product); $i++){
$order_details[] = [
'order_id' => $orders->id,
'product_id' => $product['product_id'][$i],
'units'=>4,
'quantity' => $product['quantity'][$i],
'unit_price' => $product['price'][$i],
'product_discount_id'=>0,
'amount' => $product['amount'][$i],
];
}
When I echo my array seems to look fine:
Array ( [product_id] => Array ( [0] => 7 [1] => 1 ) [quantity] => Array ( [0] => 2 [1] => 1 ) [price] => Array ( [0] => 200.00 [1] => 700.00 ) [amount] => Array ( [0] => 400 [1] => 700 ) )
The undefined offset value alternates depending on how many items from the shopping cart are passed to the loop. For example if there are 3 items the error is set to Undefined offset:3
Its like I cannot find out the real mole. I have searched the web I have not managed any way out.
Any assistance Kindly. what May I doing wrong?

The errors you are getting are about not having an element with index of count($product).
You should just use:
for($i= 0; $i <count($product)-1; $i++)
This way you loop will be fine.

That's kind of an odd array structure. You'd have a much easier time working with the data if you rearranged it like this so you have an item per product:
$products = [
[
'product_id' => 7,
'quantity' => 2,
'price' => 200.00,
'amount' => 400
],
[
'product_id' => 1,
'quantity' => 1,
'price' => 700.00,
'amount' => 700
]
];
Then you can just foreach over the products.
The fix for your problem though is this for($i= 0; $i <= count($product); $i++)

Related

How to sum array values and add total at the end in the same array

Good Afternoon,
Hello, i am new to coding and Laravel and working on home project as a self leaner. i stuck at array sum where i want to make the sum of values in array and add the same sum at the end of tjat array itself.
array:7 [▼
"2022-12-04" => array:9 [▼
"startdate" => "2022-12-04"
"Stotalbmilk" => "29.00"
"Stotala2milk" => "22.50"
"Stotaljmilk" => "20.00"
"Stotalmilk" => "71.50"
"Dtotalbmilk" => "40.00"
"Dtotala2milk" => "0.00"
"Dtotaljmilk" => "0.00"
"Dtotalmilk" => "40.00"
]
in the above array i am to add "TOTAL" at he bottom which value will be addition of 2 values ( "Stotalmilk" and "Dtotalmilk" ). Expected array will be
array:7 [▼
"2022-12-04" => array:9 [▼
"startdate" => "2022-12-04"
"Stotalbmilk" => "29.00"
"Stotala2milk" => "22.50"
"Stotaljmilk" => "20.00"
**"Stotalmilk" => "71.50"**
"Dtotalbmilk" => "40.00"
"Dtotala2milk" => "0.00"
"Dtotaljmilk" => "0.00"
**"Dtotalmilk" => "40.00"**
**"TOTAL" => "111.50"**
]
hope i properly explain my question and sorry for poor english.
Thanks in advance
So if your array have many dates with data, you can do something like:
$result = [];
foreach ($array as $date => $amounts) {
$total = (float) $amounts['Stotalmilk'] + (float) $amounts['Dtotalmilk'];
$amounts['TOTAL'] = number_format($total, 2, '.');
$result[$date] = $amounts;
}

Laravel Collection with groupby, count and sum

I'm struggling to get a groupby on a collection to work - I'm not getting the concept just yet.
I'm pulling a collection of results from a table for a player the eloquent collection will have data like this:
['player_id'=>1, 'opposition_id'=>10, 'result'=>'won', 'points'=>2],
['player_id'=>1, 'opposition_id'=>11, 'result'=>'lost', 'points'=>0],
['player_id'=>1, 'opposition_id'=>12, 'result'=>'lost', 'points'=>0],
['player_id'=>1, 'opposition_id'=>10, 'result'=>'won', 'points'=>2],
['player_id'=>1, 'opposition_id'=>11, 'result'=>'lost', 'points'=>0],
['player_id'=>1, 'opposition_id'=>10, 'result'=>'lost', 'points'=>0],
['player_id'=>1, 'opposition_id'=>12, 'result'=>'won', 'points'=>2],
I want to be able to groupBy('opposition_id') and then give me a count of results in total, total won, total lost and sum of points to end up with a collection like this:
['opposition_id'=>10, 'results'=>3, 'won'=>2, 'lost'=>1, 'points'=>4],
['opposition_id'=>11, 'results'=>2, 'won'=>0, 'lost'=>2, 'points'=>0],
['opposition_id'=>10, 'results'=>2, 'won'=>1, 'lost'=>1, 'points'=>2]
I'm trying to avoid going back to the database to do this as I already have the results from previous activity.
How can I do this using Laravel collection methods, So far all I have is:
$stats = $results->groupBy('opposition_id');
I've looked at map() but do not yet understand that method to work through a solution
Can anyone point me in the right direction please.
Happy to go back to the database if needed but assumed I could do this with the collection I already have rather than create another query. Solutions I've found on here all appear to be providing a solution in the query
Thank you
Take a look here, working code with explanation in comments.
// make a collection
$c = collect(
[
['player_id' => 1, 'opposition_id' => 10, 'result' => 'won', 'points' => 2],
['player_id' => 1, 'opposition_id' => 11, 'result' => 'lost', 'points' => 0],
['player_id' => 1, 'opposition_id' => 12, 'result' => 'lost', 'points' => 0],
['player_id' => 1, 'opposition_id' => 10, 'result' => 'won', 'points' => 2],
['player_id' => 1, 'opposition_id' => 11, 'result' => 'lost', 'points' => 0],
['player_id' => 1, 'opposition_id' => 10, 'result' => 'lost', 'points' => 0],
['player_id' => 1, 'opposition_id' => 12, 'result' => 'won', 'points' => 2]
]
);
// this only splits the rows into groups without any thing else.
// $groups will be a collection, it's keys are 'opposition_id' and it's values collections of rows with the same opposition_id.
$groups = $c->groupBy('opposition_id');
// we will use map to cumulate each group of rows into single row.
// $group is a collection of rows that has the same opposition_id.
$groupwithcount = $groups->map(function ($group) {
return [
'opposition_id' => $group->first()['opposition_id'], // opposition_id is constant inside the same group, so just take the first or whatever.
'points' => $group->sum('points'),
'won' => $group->where('result', 'won')->count(),
'lost' => $group->where('result', 'lost')->count(),
];
});
// if you don't like to take the first opposition_id you can use mapWithKeys:
$groupwithcount = $groups->mapWithKeys(function ($group, $key) {
return [
$key =>
[
'opposition_id' => $key, // $key is what we grouped by, it'll be constant by each group of rows
'points' => $group->sum('points'),
'won' => $group->where('result', 'won')->count(),
'lost' => $group->where('result', 'lost')->count(),
]
];
});
// here $groupwithcount will give you objects/arrays keyed by opposition_id:
[
10 => ["opposition_id" => 10,"points" => 4,"won" => 2,"lost" => 1]
11 => ["opposition_id" => 11,"points" => 0,"won" => 0,"lost" => 2]
12 => ["opposition_id" => 12,"points" => 2,"won" => 1,"lost" => 1]
]
// if you use $groupwithcount->values() it'll reset the keys to 0 based sequence as usual:
[
0 => ["opposition_id" => 10,"points" => 4,"won" => 2,"lost" => 1]
1 => ["opposition_id" => 11,"points" => 0,"won" => 0,"lost" => 2]
2 => ["opposition_id" => 12,"points" => 2,"won" => 1,"lost" => 1]
]

ElasticSearch Aggregation Past _source

I am trying to get a list of all possible article groups. My Product array example is like so:
[8] => Array
(
[_index] => product_index
[_type] => product_51_DEU
[_id] => AV7mxnScT3P3M-G9u9aK
[_score] => 1
[_source] => Array
(
[artikelnummer] => G123456
[produktname] => My Cool Name Here
[artikeltext] => BLA
[produktgruppe] => Car Products
[anwendungsbezeichnung] => Wash Products
[lieferant] => Turtle
)
)
My PHP search parameter looks like this:
$mainMenuParams = [
'index' => 'product_index',
'type' => 'product_51_DEU',
'body' => [
'aggs' => [
'_SOURCE' => [
'terms' => [
'field' => '_source.produktgruppe'
]
]
]
]
];
$listProduktGroup = $GLOBALS["client"]->search($mainMenuParams);
I get an answer but there are no aggregations. I have tried many combinations, but none seem to work. Anyone have any idea where this is wrong? I want to see an aggregation with an output of all of the possible [produktgruppe]. There are 10 Groups in all, but it would be nice to see this in the results and then maybe even a count of all products in each group.
If I do the exact same query on "_types" I get accurate results.

Getting data from Laravel Collection

I realize this is a dumb question, but I can't find an answer for it. I am missing some understanding around how collections work
I am querying a google places API for information about a location
$response = GooglePlaces::textSearch('10 Downing Street', $optionnalParameters);
This works and gives me a response, which I can var_export:
Illuminate\Support\Collection::__set_state(array(
'items' =>
array (
'html_attributions' =>
array (
),
'results' =>
Illuminate\Support\Collection::__set_state(array(
'items' =>
array (
0 =>
array (
'formatted_address' => '10 Downing St, London SW1A 2AA, United Kingdom',
'geometry' =>
array (
'location' =>
array (
'lat' => 51.503363499999999,
'lng' => -0.12762480000000001,
),
'viewport' =>
array (
'northeast' =>
array (
'lat' => 51.503432400000001,
'lng' => -0.12551999999999999,
),
'southwest' =>
array (
'lat' => 51.503156800000014,
'lng' => -0.12832640000000001,
),
),
),
'icon' => 'https://maps.gstatic.com/mapfiles/place_api/icons/civic_building-71.png',
'id' => 'ffd85a3e543406e34703ee947c73ef54f5e167fe',
'name' => '10 Downing Street',
'photos' =>
array (
0 =>
array (
'height' => 2534,
'html_attributions' =>
array (
0 => 'Christopher Chan',
),
'photo_reference' => 'CoQBdwAAAHdqDNRUhalI7Ko_YXqckWM5M7I1IeD0xXOvjnxruS4BYCFnt99lCEy5xQJh7XtTvGTfZbKlnVhbxaJ_OloLxaPyoInqIpgRY-3LyB3Q70tDX3izeraFEM4Bw-ExmRzz6h18iMQlKb0DoDXnW26uO4RR-7YFjPNi6M0y5D7cmrl9EhAmI7GerM-TXpKD3BVVTtOWGhQZotS0ZNI2nK9G7jXxDEyvoQ5IxQ',
'width' => 3801,
),
),
'place_id' => 'ChIJRxzRQcUEdkgRGVaKyzmkgvg',
'rating' => 3.3999999999999999,
'reference' => 'CmRSAAAAh-Drhh9G_EW3azxZSikW_jR-ZjI2lhZTw6MfWqh9EiTaCEy4uW2okv_g6QfaKoupoeDu3DpfS6MjIvUvp6cc2uAoLlyH9NbkMrnQg9q3ED3R91OejmAScjRhe8G47kJ1EhD_oYVFakm4n6I27j-8iN6oGhQVH6rv1t6uGQBVVOqy1fCAB17JOg',
'types' =>
array (
0 => 'point_of_interest',
1 => 'establishment',
),
),
),
)),
'status' => 'OK',
),
))
I want to get the formatted_address element from the collection. it appears under the results element, that in turn has a collection under it with elements Items,0,formatted_address.
when i start trying to 'walk down' to the element i require I get index not found for Items
var_export($response['results']['items']);
so essentially, how am I supposed to interact with collections? i have tried google but most the links tell me about the extra functions that collections provide and not how to get data from it
any help greatly appreciated.
I think https://laravel.com/docs/5.3/collections#method-all explains what you are missing. You want to call
->all()
on your Collection to return the underlying array represented by the collection.
Than you can access fields in the returned array.

Magento solr search - how to add custom field to product search index

I have enterprise magento 1.10 and I need to add custom field to solr index in order to to have better search results. This custom field should contain category names for indexed product.
I need to know which part of magento I need to amend. I modified method Enterprise_Search_Model_Resource_Index::_getCatalogCategoryData which actualy produces some category related data but my new field doesnt appear in message to sorl.
Have I miss something?
Also I have some notes here, maybe it would someone help http://docs.nostresscommerce.com:8090/pages/viewpage.action?pageId=2490371
My output of rewrited Enterprise_Search_Model_Resource_Index::_getCatalogCategoryData, and I need category_name_level_* in solr.
[result] => Array
(
[12227] => Array
(
[#categories] => Array
(
[0] => 2
[3] => 3235
[7] => 2765
[10] => 6672
)
[#show_in_categories] => Array
(
[1] => 6671
[2] => 6670
[4] => 3224
[5] => 3199
[6] => 3191
[8] => 2761
[9] => 2760
)
[#position_category_2] => 1
[#position_category_6671] => 120000
[#position_category_6670] => 120000
[#position_category_3235] => 0
[#position_category_3224] => 150000
[#position_category_3199] => 150000
[#position_category_3191] => 120000
[#position_category_2765] => 1
[#position_category_2761] => 250001
[#position_category_2760] => 250001
[#position_category_6672] => 0
[#visibility] => 4
[#category_name_level_1] => Array
(
[0] => BCC Root Category
)
[#category_name_level_4] => Array
(
[0] => Huishouden
[1] => Top wasmachines
[2] => Accessoires wasmachine
)
[#category_name_level_3] => Array
(
[0] => Beste Koop Shop
[1] => Top
[2] => Wasmachine
)
[#category_name_level_2] => Array
(
[0] => Acties
[1] => Speciale informatie pagina's
[2] => Huishouden
)
[#category_name_level_5] => Array
(
[0] => Wasmachines
)
)
)
Thanks a lot,
Jaro.
I found a solution
class Xyz_Solrsearch_Model_Resource_Engine extends Enterprise_Search_Model_Resource_Engine {
public function __construct()
{
$this->_advancedDynamicIndexFields[] = '#category_name_level_';
parent::__construct();
}
}

Resources