laravel get data without applying casts

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Laravel: Getting Raw Data Without Applying Model Casts As a senior developer working with Laravel and Eloquent, one of the most common points of friction developers encounter is when data retrieval seems to behave differently than expected. A frequent source of confusion arises when dealing with model casts—specifically date and datetime formats. You want the raw database value, but Eloquent is automatically transforming it for you. This post dives deep into how you can retrieve all your model data without triggering those automatic type conversions, allowing you to handle the formatting exactly as required by your application logic. ## The Problem: Model Casts Hiding Raw Data Let's set up the scenario you described. You have an Eloquent model where you've defined casts for date fields: ```php class Post extends Model { protected $casts = [ 'created_at' => 'datetime:D, M d Y', // Formats it as '15 Jan 2023' 'updated_at' => 'datetime:D, M d Y', ]; } ``` When you execute a simple query, Eloquent intercepts the result and applies this formatting automatically. ```php $posts = Post::all(); // $posts now contains objects where created_at is a formatted string ('15 Jan 2023'), not a true DateTime object or a standard timestamp. ``` You want to retrieve the raw timestamp value (e.g., the ISO 8601 format or a native Carbon instance) without this presentation layer applied by the model. ## Solution 1: Accessing Attributes Directly from the Collection The most direct and pragmatic way to bypass the casting mechanism for specific attributes is to access the underlying attribute directly on the collection items, rather than relying solely on the standard accessor methods provided by Eloquent. When you use `Model::get()->toArray()`, Eloquent still processes the data through its internal hydration layer, which includes casts. To get the raw value, you need to access the attributes *before* or *after* Laravel's automatic transformation has occurred, or leverage raw query methods. However, for simple retrieval of dates that are stored natively in the database (as `DATETIME` or `TIMESTAMP`), accessing the attribute directly often yields the best result if you are managing the data format yourself post-retrieval: ```php $posts = Post::all(); // Accessing the raw attribute value from the collection items foreach ($posts as $post) { // Accessing the attribute directly, bypassing model casting for this specific field $rawDate = $post->created_at; // $rawDate will contain the native Carbon instance or SQL string if configured correctly, // depending on how Eloquent hydrates it internally. } // If you need an array of raw timestamps: $rawData = Post::all()->map(function ($post) { return [ 'id' => $post->id, 'created_at' => $post->created_at, // This is the value directly from the model ]; }); ``` While this method doesn't entirely remove the *existence* of the cast definition in the model, it allows you to pull the underlying data structure. For complex scenarios involving custom serialization or mass assignment where casting causes issues, this direct access pattern becomes crucial. This aligns with best practices when dealing with data integrity across your Laravel application, as emphasized on resources like [laravelcompany.com](https://laravelcompany.com). ## Solution 2: Using Raw SQL for Full Control If the goal is absolute control over the output format and you are retrieving a large dataset where Eloquent overhead might be unnecessary, falling back to raw SQL queries provides the highest level of control. You can fetch the data exactly as the database stores it, completely ignoring model-level casting rules. ```php // Retrieve data directly from the database without Eloquent hydration/casting $sql = "SELECT * FROM posts"; $results = \DB::select($sql); // $results will be an array of standard PHP objects or arrays, // with 'created_at' containing the native MySQL DATETIME string. ``` This approach is powerful when performance is critical or when you need data structures that don't rely on Eloquent's automatic hydration features. It gives you direct access to the raw data from your database, which is often the ultimate source of truth. ## Conclusion In summary, while model casts are excellent for ensuring consistent presentation layers across your application (making dates look nice for views), they can sometimes impede raw data retrieval needs. For scenarios where you need the unformatted, native datetime value, leveraging direct attribute access on the collection or falling back to raw SQL queries provides the necessary control. Always choose the method that best balances developer convenience with the specific requirements of your data flow.