Laravel creating our first database table

β€” 4 minute read

permalink

A while ago, I wrote an article on setting up a Laravel project but never got on to writing more Laravel stuff.

Today I'll be going through some important things, Databases! Laravel has a very cool way of creating/maintaining databases, which is in the form of migrations.

It basically means every time you need to make a change to a database, you'll need to write a new migration. Yes, even if you want to change something on an old table.

And that has many benefits. It tells laravel what the old field was, you can auto set new values, and much more.

Another great thing about migration is the option to roll back a migration. Using the rollback will revert the migration as you describe in the migration file.

Setting the environment file permalink

Before we can work with the database, we have to create a connection first. These connections are maintained and defined in a so-called .env file.

Each Laravel project will come with an example called the .env.example.

Copy the example to your own version and change the variables to be correct.

For today we are just looking at the database section.

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laravel_app
DB_USERNAME=root
DB_PASSWORD=
FORWARD_DB_PORT=3356

Creating our first Laravel migration permalink

Enough talk. Let's see how this works. Open your favorite terminal and navigate to your project root folder.

php artisan make:migration create_books_table

Running this command will create a new migrations file, you can find under database/migrations/{time}_create_books_table.php.

You might see there are already the default migration files from Laravel in this folder.

Open up the newly created file.

You will see there are two sections here, an up and a down. As mentioned above, the up will be used to create or alter tables. The down will be used when the rollback is called and generally should revert what happened in the up.

Let's add some fields to our table.

Schema::create('books', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->number('year');
$table->timestamps();
});

This will create a table called books that has the following fields.

  • id: auto-increment field
  • title: string
  • year: number
  • created_at: datetime
  • updated_at: datetime

The id and timestamps are by default added by Laravel.

Now, we can run the following command to run our new migration:

./vendor/bin/sail artisan migrate

If you haven't used the new start, you can run:

php artisan migrate

This will create the table for us, and it should look something like this:

Laravel database table created

And there you go, our first database table in Laravel.

Thank you for reading, and let's connect! permalink

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter