Skip to main content
Tutorial Free

Two ways to set default DB column values in Laravel

July 06, 2016
1 min read
Sometimes there is a need to set a default column value for database field - if it's not provided when creating the entry. There are two simple ways of doing it. 1. Database migrations Of course, you can set that up on the database level, which is probably the most correct - then you don't depend on Laravel code at all, database takes care of it for you. That's how you write a field migration:
Schema::table('users', function ($table) {
    $table->string('email')->default('[email protected]');
});
2. Model attribute Another way of doing this is in your Laravel models - you can add attributes and set default values there:
class Country extends Model {
    protected $attributes = [
        'currency' => 'USD'
    ];
}
So choose your own way, hope that's helpful!

Enjoyed This Tutorial?

Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.

Recent Courses

Laravel Modules and DDD

16 lessons
1 h 59 min

Filament 4 From Scratch

28 lessons
2 h 25 min

Claude Code for Laravel Projects: Crash Course

8 lessons
48 min

Comments & Discussion

No comments yet…

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.