This is my solution. I wrote this inside my Model class. (ratings is a string type)
$code = (int)$ratings;
But I need to change $ratings when retrieving it from the database. how can I do it?
From Laravel Documentation:
Attribute Casting The $casts property on your model provides a convenient method of converting attributes to common data types. The $casts property should be an array where the key is the name of the attribute being cast and the value is the type you wish to cast the column to. The supported cast types are integer, real, float, double, decimal:, string, boolean, object, array, collection, date, datetime, and timestamp. When casting to decimal, you must define the number of digits (decimal:2).
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'ratings' => 'integer',
];
}
Attributes that are null will not be cast. In addition, you should never define a cast (or an attribute) that has the same name as a relationship.