Im trying to create a Refinement List with the following product data:
- Table 1: Product
- Table 2: Product Details.
With Table Product, all the desired data is retrieved perfectly creating a json file very similar to algolia ecommerce best buy example.
{
“id”: "3953367"
“name”: “360fly - Panoramic 360° HD Video Camera - Black”,
“popularity”: 9999,
“rating”: 5,
“objectID”: “9131042”
},
On the other hand, Table 2 -> Product Details have the following structure:
id - productId - name - value.
1 - 3953367 - Operating System - Android 4.4 KitKat
2 - 3953367 - Voice Activated - Yes
3 - 3953367 - Processor Speed - 1.2 gigahertz
As you can see, 1 single product can display more than 3 options for facet.
- Operating System
- Voice Activated
- Processor Speed
But I dont know how to structure that data to create a refinement list like this:
As an example, how can I create the json to allow people to refine using Operating System.
I tried something like:
$record[$this->productdetails->map(function($data) {return [$data[‘name’]];})->toArray()]
=
$this->productdetails->map(function($data) {return [$data[‘value’]]; })->toArray();
but in this example i receive error:
Illegal offset type
Any Example Appreciated. Im using Laravel Scout with Algolia.
Based on user @daniel-h approach I updated my code.
public function toSearchableArray()
{
$record['name'] = $this->productName;
$record['description'] = $this->productSpecification;
$record['brand'] = $this->productBrand;
$record['color'] = $this->color;
$new_array = [];
foreach ($this->productdetails as $record) {
$new_array[$record['name']] = $record['value'];
}
return $record;
}
Currently Im receiving array_merge(): Argument #1 is not an array
UPDATE:
the solution was:
// Add the product details to the array
foreach ($this->productDetails as $detailsRecord) {
$record[$detailsRecord->name] = $detailsRecord->value;
}