Laravel creating our first database table

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

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.
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
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.
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:

And there you go, our first database table in Laravel.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter