Part 6 - Create Posts table using a Migration [How to use TDD to build a REST API in Laravel 6]

Опубликовано: 31 Октябрь 2024
на канале: StudyStream+
794
8

We are going to need a table to store our data for Posts. Let’s create our Posts table. To do this, we are going to create a migration. If you are new to migrations, you can think of them as a sort of version control for your database.

Laravel keeps migrations in: Database - Migrations. There are some default ones here. Our project won’t require these tables. We can delete these migrations.

Let’s open up our terminal and run “php artisan”. This will give you a list of commands available to us. We need the make migration command. Run “php artisan make:migration -h”. This will give us more information about this command. We need to run “php artisan make:migration create_posts_table”.

This is a good convention to adhere to. It explicitly states what the migration does. For some reason this command does take a while to complete.

We can now see our new migration in the migrations folder. Laravel has some boiler plate in here. Let’s expand it. We have access to the migration helper methods via the table object. We are going to use these methods, to define our columns.

When we create an up method, we must create a down method. All we would have to do, is drop the table, the migration would have created. Laravel has already added this as part of the boilerplate.

Before we test our migration, we need to check our .env file. Our .env file needs to be updated to the correct Database name and credentials.

To test our migration, let’s ssh into our virtual machine. We need to run the command: vagrant ssh. I have “vs” as an alias, so I will be using this from now on. We need to cd into our code directory.

This time, let’s run “php artisan migrate”. This will run our migration for us to create our table. We can confirm the table has been created using PhpStorm.

Watch next video:
   • Part 7 - Create Post Model and Factor...  

Watch this course in full here:
   • How to use TDD to build a REST API in...  

Laravel Tutorials:
   • Laravel PHP Tutorials  

2 - 3 min Developer Quick Tips Series:
https://www.youtube.com/playlist?list... this course in full here:
   • How to use TDD to build a REST API in...