# Laravel migrations: create and modify a database schema

> Learn how to use Laravel migrations to create and modify your database schema with php artisan make:migration and migrate, and roll back changes when needed.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2023-06-13 | Topics: [Laravel](https://flaviocopes.com/tags/laravel/) | Canonical: https://flaviocopes.com/how-to-use-migrations-to-create-and-modify-the-database-schema/

Migrations are excellent for handling changes to the database, so you can apply them, and roll back in case you need to restore to a previous state of your data.

From the terminal, stop the Laravel server and run this command to create a new _migration_, which is what we will use to create the database table(s) we need to use:

```php
php artisan make:migration initial_table_creation
```

![Terminal output showing the creation of a new migration file with timestamp](https://flaviocopes.com/images/how-to-use-migrations-to-create-and-modify-the-database-schema/1.webp)

This command created the `2023_05_11_080724_initial_table_creation.php` file (the date and time will of course change for you) in the `database/migrations` folder.

![Database migrations folder showing the newly created migration file alongside existing Laravel auth migrations](https://flaviocopes.com/images/how-to-use-migrations-to-create-and-modify-the-database-schema/2.webp)

Notice there are other migrations, which are added by the Laravel framework itself for it’s authentication system. 

But let’s focus on creating a new table, let’s call it `dogs`.

Go in the `up()` function of the migration we creataed.

Let’s create a `dogs` table with 3 columns, an `id`, a `name` string and the timestamp utility columns (`created_at` and `updated_at`, as we’ll see).

```php
Schema::create('dogs', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->timestamps();
});
```

![Migration file code showing Schema::create for dogs table with id, name, and timestamps columns](https://flaviocopes.com/images/how-to-use-migrations-to-create-and-modify-the-database-schema/3.webp)

Now from the terminal run the command 

```php
php artisan migrate
```

And Laravel will apply the migrations that have not been applied yet, which at this point means all the migrations you see in the `migrations` folder:

![Terminal output showing php artisan migrate command executing and applying all pending migrations](https://flaviocopes.com/images/how-to-use-migrations-to-create-and-modify-the-database-schema/4.webp)

If you open the `database/database.sqlite` file using a database visualization tool like [TablePlus](https://tableplus.com/) (free version, available for all operating systems) you will see the newly created tables, including the one we defined:

![TablePlus database viewer showing the SQLite database with newly created tables including the dogs table](https://flaviocopes.com/images/how-to-use-migrations-to-create-and-modify-the-database-schema/5.webp)

If you do a mistake in a migration, you can rollback any change in a migration using 

```php
php artisan migrate:rollback
```

and this rolls back the latest changes you did to the database.

Find more about migrations on the [official migrations guide](https://laravel.com/docs/10.x/migrations).
