Set a default value for JSON fields in Laravel migrations
April 19, 2022 ‐ 1 min read
Laravel allows you to create JSON fields in migrations, which are supported in most up-to-date relational databases. In the "simpler" SQLite database, that I often use in early stages of a new project, JSON is stored as ordinary text.
And since you can create JSON columns in your migrations you may want to populate them with a default value in your migration too. This might work a little differently than expected. Since you're interacting with the database table directly instead of via a model you can't take advantage of Laravel's field casting in your migrations.
Instead you need to provide valid JSON as a default value, using the
json_encode() function in PHP is of great use here.
If you prefer you can directly enter a string as well, which is what
json_encode() does for you.