Laravel Eloquent: Custom Casts
December 24, 2025
•
1 min read
•
78 views
Table of Contents
Create custom casts:
Define Cast
class Json implements CastsAttributes
{
public function get($model, $key, $value, $attributes)
{
return json_decode($value, true);
}
public function set($model, $key, $value, $attributes)
{
return json_encode($value);
}
}
Use in Model
protected $casts = [
'options' => Json::class,
];
Access Naturally
$post->options = ['featured' => true];
$post->save();
Related Posts
Laravel Sanctum API Authentication Complete Guide
Build secure API authentication with Laravel Sanctum for SPAs and mobile apps.
Laravel Rate Limiting: Protect Your Application
Implement rate limiting to protect your Laravel application from abuse.
Laravel Blade Components: Build Reusable UI
Create powerful reusable components with Laravel Blade.
Comments (0)
No comments yet. Be the first to comment!