It looks like you're encountering an issue with the type casting in your Eloquent model. The problem might be due to the way you defined the casts method. In Laravel, the casts property should be defined as a public property, not as a method.
Here's how you can properly define the casts property in your model:
class ProjectsCtcWorksheet extends Model
{
protected $casts = [
'original_contract' => 'decimal:2',
];
}
Make sure that you are not defining casts as a method but as a property. This should ensure that the original_contract attribute is cast to a decimal with 2 decimal places.
Here is a complete example:
use Illuminate\Database\Eloquent\Model;
class ProjectsCtcWorksheet extends Model
{
protected $casts = [
'original_contract' => 'decimal:2',
];
}
After making this change, when you retrieve the original_contract attribute, it should be cast to a decimal as expected:
$worksheet = ProjectsCtcWorksheet::with('items')->where('id', $id)->firstOrFail();
dd(gettype($worksheet->original_contract)); // This should now output "double" or "float"
If you still encounter issues, ensure that your database column is indeed a decimal type and that there are no other parts of your code that might be interfering with the casting. Additionally, you can try clearing your configuration cache:
php artisan config:cache
php artisan config:clear
This should resolve the issue and ensure that your attribute is properly cast to a decimal.