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 on Laravel Daily

Laravel 13 Starter Kit Teams and Customizations

10 lessons
33 min

Laravel 13 Eloquent: Expert Level

41 lessons
1 h 34 min

Queues in Laravel 13

18 lessons
1 h 12 min read

No comments yet…