title: Laravel Array & JSON Casting to Algolia
body: <p>I am trying to send some data along to Algolia through the toSearchableArray. Any strings I have stored in my DB are sending along fine, but I hit a roadblock when trying to push nested JSON data along—the information is being sent as a string with characters escaped.</p>
  
  <p>This is a sample of the nested object that I am storing in my table (MySQL with a JSON data type):</p>
  
  <pre><code>[
      {
          "id": 19, 
          "name": "Mathematics", 
          "short": "Math"
      }, 
      {
          "id": 23, 
          "name": "Science", 
          "short": "Science"
      }, 
      {
          "id": 14, 
          "name": "Health and Life Skills", 
          "short": "Health"
      }
  ]
  </code></pre>
  
  <p>My model looks like this:</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  use Laravel\Scout\Searchable;
  
  class Resource extends Model
  {
      use Searchable;
      protected $primaryKey = 'objectID';
  
      public function toSearchableArray()
      {
          $data = $this-&gt;toArray();
  
          $data['grades'] = explode(';', $data['grades']);
          $data['units'] = explode(';', $data['units']);
  
          return $data;
      }
  }
  </code></pre>
  
  <p>I get an output that looks like this:</p>
  
  <pre><code>array:22 [
    "objectID" =&gt; 1
    "name" =&gt; "Resource #1"
    "slug" =&gt; "resource-1"
    "write_up" =&gt; """
      This is an example write up.
      """
    "author" =&gt; "johnny"
    "type_name" =&gt; "Lesson Plan"
    "language" =&gt; "English"
    "grades" =&gt; array:3 [
      0 =&gt; "Kindergarten"
      1 =&gt; "Grade 1"
      2 =&gt; "Grade 4"
    ]
    "subjects" =&gt; "[{"id": 19, "name": "Mathematics", "short": "Math"}, {"id": 23, "name": "Science", "short": "Science"}, {"id": 14, "name": "Health and Life Skills", "short": "Health"}]"
    "units" =&gt; array:2 [
      0 =&gt; "Unit A"
      1 =&gt; "Unit B"
    ]
    "main_image" =&gt; "https://dummyimage.com/250x325/000000/fff.png&amp;text=Just+a+Test"
    "loves" =&gt; 88
    "downloads" =&gt; 280
    "created_at" =&gt; "2018-01-01 13:26:47"
    "updated_at" =&gt; "2018-01-02 10:10:32"
  ]
  </code></pre>
  
  <p>As you can see, the 'subjects' attribute is being stored as a string. I know there is attribute casting in 5.5 (I am running 5.5), but I am not too clear on how I would implement the example they have for Array &amp; JSON Casting in my work above. <a href="https://laravel.com/docs/5.5/eloquent-mutators#attribute-casting" rel="nofollow noreferrer">https://laravel.com/docs/5.5/eloquent-mutators#attribute-casting</a></p>
  
  <p>Would anyone be willing to show me an example?</p>