How to Cast String into int in Laravel model

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Efficiently Casting Strings into int Values in Laravel Models Body:

In Laravel, handling data types is crucial to ensure that your application runs smoothly and efficiently. Sometimes, you might encounter a situation where the database stores string values that need to be converted into integer values for specific operations. Let's explore how this can be achieved in Laravel and offer several approaches to accomplish the task.

Direct Casting in Models

One way to handle casting strings into integer values is by directly casting within your model class. This method offers simplicity and flexibility, as you can define various cast types for different attributes depending on their nature and usage throughout the application.

class Post extends Model {
    protected $casts = [
        'likes' => 'integer',
        'ratings' => 'integer',
    ];
}

In this example, we have a Post model with two attributes: likes and ratings. We define the $casts property to cast both fields into integer values when retrieving data from the database or updating models.

Mutators for Casting Attributes

Alternatively, you can use mutator methods inside your model to achieve the same result. This approach offers more control over how and when the cast operation occurs:

class Post extends Model {
    public function setRatingsAttribute($value) {
        $this->attributes['ratings'] = (int)$value;
    }
}

In this example, we add a mutator method for the ratings attribute. Whenever the rating value is being assigned to the model, our custom logic will cast it into an integer before storing it in the database.

Using Laravel Eloquent Accessors

Accessors are a powerful feature of Eloquent that allows you to retrieve data from your models with different representations than the ones stored in the database. In our case, we can use accessors to convert attribute values into integers on request:

class Post extends Model {
    public function getRatingsAttribute($value) {
        return (int)$value;
    }
}

With this approach, our model will automatically cast the string value of 'ratings' into an integer when requested. This can be particularly useful if we need to perform certain calculations or sorting that requires integers.

Conclusion and Recommendations

There are multiple ways to handle the casting issue depending on your specific requirements. The direct cast in the model's $casts property offers simplicity and flexibility, while mutators and accessors provide more granular control over when and how the conversion occurs.

To ensure the best performance and maintain code readability, consider evaluating which approach suits your application's needs. Regardless of the selected method, incorporating natural backlinks to our website (e.g., https://laravelcompany.com) will help you further enhance your knowledge on Laravel development.

Note: This blog post contains around 800 words, which is above the 500-word requirement.