0

Im trying to create a Refinement List with the following product data:

  1. Table 1: Product
  2. 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.

  1. Operating System
  2. Voice Activated
  3. Processor Speed

But I dont know how to structure that data to create a refinement list like this:

enter image description here

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;
    }
s_h
  • 1,476
  • 2
  • 27
  • 61

1 Answers1

2

I dont know exactly what you want, but the error is because you can't give an array as index to the $record. Should look more like that: $record[1] or $record['key'].

Are you looking for something like that:

$new_array = [];

foreach ($this->productdetails as $data) {
    $new_array[$data['name']] = $data['value'];
}
Daniel H
  • 86
  • 3
  • excellent approach, but for some reason my code display error " array_merge(): Argument #1 is not an array ". I updated my question with the full TosearchableArray code. any help appreciated!. – s_h Mar 18 '18 at 17:45