title: Laravel 5.4 save json to database
body: <p>help me with save json to db. Table field type - text. 
  I have Model with cast array</p>
  
  <pre><code>class Salesteam extends Model 
  {
      protected $casts = [        
          'team_members' =&gt; 'array'
      ];
  }
  </code></pre>
  
  <p>I want get json like this {"index":"userId"} and store it to db.
  Example:</p>
  
  <pre><code>{"1":"7","2":"14","3":"25","4":"11"}
  </code></pre>
  
  <p>I have Salesteam Model and 'team_members' column in db to saving all user id's belong to this team. 
  My code find team by id, get all 'team_members' attribute and add new user to this team. I want store all users in json format and incrementing index when add new user id.</p>
  
  <p>Salesteam attributes:</p>
  
  <pre><code>"id" =&gt; 20
  "salesteam" =&gt; "Admin"
  "team_leader" =&gt; 0
  "invoice_target" =&gt; 884.0
  "invoice_forecast" =&gt; 235.0
  "team_members" =&gt; "[1,66,71]"
  "leads" =&gt; 0
  "quotations" =&gt; 0
  "opportunities" =&gt; 0
  "notes" =&gt; ""
  "user_id" =&gt; 1
  "created_at" =&gt; "2017-09-22 09:13:33"
  "updated_at" =&gt; "2017-09-22 13:10:25"
  "deleted_at" =&gt; null
  </code></pre>
  
  <p>Code to add new user to team:</p>
  
  <pre><code>$salesTeam = $this-&gt;model-&gt;find($salesTeamId);
  $teamMembers = $salesTeam-&gt;team_members;
  $teamMembers[] = $userId;
  $salesTeam-&gt;team_members = $teamMembers;
  $salesTeam-&gt;save();
  </code></pre>
  
  <p>But it stores to db array and without index</p>
  
  <pre><code>"team_members" =&gt; "[1,34,56]"
  </code></pre>
  
  <p>From Laravel documentation - <a href="https://laravel.com/docs/5.4/eloquent-mutators" rel="noreferrer">Array &amp; JSON Casting</a> "array will automatically be serialized back into JSON for storage"</p>
  
  <p>Where i'm wrong?</p>