title: Laravel eloquent model model attribute casting (Which attribute types should I cast?)
body: <p>I am not sure I fully understand Laravel Eloquent attribute casting. According to documentation, (<a href="https://laravel.com/docs/8.x/eloquent-mutators#attribute-casting" rel="nofollow noreferrer">https://laravel.com/docs/8.x/eloquent-mutators#attribute-casting</a>), these are the supported types:</p>
  <p>integer, real, float, double, decimal:, string, boolean, object, array, collection, date, datetime, timestamp, encrypted, encrypted:object, encrypted:array, and encrypted:collection</p>
  <p>So far, I've only used date casting on my models (when the fields were stored as timestamps in the db), like this:</p>
  <pre><code>protected $dates = [
      'modified_at', 'published_at'
  ];
  </code></pre>
  <p>I also understand the need for attribute casting to boolean when the values are stored as integers (0 or sth else).</p>
  <p>But what about other attribute types (integers, for example), should I always do attribute casting? Or just when the field in the database is of a different type? What are the use cases or what is the best practice with other attributes?</p>
  <p>(I can't, for example, imagine creating a string field in migrations, then saving some number inside it as string and then casting it back into an integer on the model?)</p>