title: Laravel convert JSON record to Class instance
body: <p>I have a transaction record that contains a <code>JSON</code> column containing a <code>Client</code> record.</p>
  
  <pre><code>$t-&gt;client
  
  {'id': 12, 'name': 'John'....}
  </code></pre>
  
  <p>I have a <code>Client</code> class</p>
  
  <p>How do I instantiate a new Client class and assign the <code>$t-&gt;client</code> <code>JSON</code> values to it?</p>
  
  <pre><code>$billedClient = new Client;
  $billedClient = collect($t-&gt;client);
  </code></pre>
  
  <p><strong>UPDATE:</strong> the field contains data stored as <code>JSON</code> but in the <code>Transaction</code> class it is cast as JSON, so Laravel is converting it to <code>Array</code> by the time is it accessed in the code.</p>
  
  <p><strong>UPDATE 2:</strong> I have changed my code based on @mega6382's suggestion below but it fails.</p>
  
  <pre><code>$data = $transaction-&gt;client;
  $client = new \App\Models\Client();
  $client-&gt;set($data); 
  </code></pre>
  
  <p>This returns an error in <code>Model.php</code> of <code>preg_match() expects parameter 2 to be string, array given</code></p>
  
  <p><strong>UPDATE 3:</strong> Solution was as suggested below without the <code>json_encode</code>.</p>
  
  <pre><code>$clientCollection = Client::hydrate([$t-&gt;client]);
  $client = $clientCollection-&gt;first();
  </code></pre>