justice
justice

Reputation: 85

Change date format in Laravel migration

I want to change the date format from 1990-01-30 to 30/01/1990 straight from my migration. I get the following error when I try to migrate with seeding from factory.

Invalid datetime format: 1292 Incorrect date value: '30/01/1990' for column 'dob' at row 1

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('fname');
        $table->string('lname');
        $table->string('phone')->unique();
        $table->date('dob')->format('d/m/Y');
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

Upvotes: 2

Views: 17579

Answers (2)

Anil Stha
Anil Stha

Reputation: 581

Declare in model:

class ModelName extends Model
{      

 protected $casts = [
    'created_at' => 'datetime:d/m/Y', // Change your format
    'updated_at' => 'datetime:d/m/Y',
];
}

Upvotes: 2

user633440
user633440

Reputation:

You can't do that within a migration. You will need to use Carbon and format the date in your Model instead.

Upvotes: 1

Related Questions