beginnerLaravel
What is attribute casting?
Answer
Clear, interview-ready explanation
Attribute casting tells Eloquent how to convert a stored value when it is accessed on a model and how to prepare it for storage. Common casts include boolean, integer, decimal, array, JSON, date, datetime, enum, encrypted, and custom cast classes.
For example, a database value of 0 or 1 can be exposed as a PHP boolean, and a JSON column can be exposed as an array. Casts keep type handling consistent at the model boundary, reducing repetitive conversion code and avoiding surprises caused by database strings or numeric representations.