Masonite Documentation
v1.6
v1.6
  • Introduction
  • Prologue
    • Introduction and Installaton
    • Contributing Guide
    • How To Contribute
    • Release Cycle
    • Known Installation Issues
  • What's New
    • Masonite 1.3
    • Masonite 1.4
    • Masonite 1.5
    • Masonite 1.6
  • Upgrade Guide
    • Masonite 1.3 to 1.4
    • Masonite 1.4 to 1.5
    • Masonite 1.5 to 1.6
  • The Basics
    • Routing
    • Controllers
    • Views
    • Requests
    • Static Files
    • Helper Functions
  • The Craft Command
    • Introduction
    • Creating Commands
    • Authentication System
  • Architectural Concepts
    • Request Lifecycle
    • Service Providers
    • Service Container
  • Advanced
    • Middleware
    • Validation
    • Creating Packages
    • Extending Classes
    • Creating a Mail Driver
    • Sessions
  • Useful Features
    • Template Caching
    • Mail
    • Uploading
    • View Composers and Sharing
    • Caching
    • Broadcasting
    • Queues and Jobs
    • Compiling Assets
    • Framework Hooks
  • Security
    • Authentication
    • Encryption
    • CSRF Protection
  • Orator ORM
    • Basic Usage
    • Query Builder
    • ORM
    • Pagination
    • Schema Builder
    • Database Migrations
    • Collections
  • Managers and Drivers
    • About Managers
    • About Drivers
    • Contracts
  • Official Packages
    • Masonite Entry
    • Masonite Billing
  • Creating Your First Blog
    • Introduction
    • Part 1 - Creating Our First Route
    • Part 2 - Creating Our First Controller
    • Part 3 - Authentication
    • Part 4 - Migrations
    • Part 5 - Models
    • Part 6 - Designing Our Blog
    • Part 7 - Showing Our Posts
    • Part 8 - Updating and Deleting Posts
Powered by GitBook
On this page
  • Getting Started
  • Craft Command
Edit on Git
Export as PDF
  1. Creating Your First Blog

Part 4 - Migrations

PreviousPart 3 - AuthenticationNextPart 5 - Models

Last updated 7 years ago

Getting Started

Now that we have our authentication setup and we are comfortable with migrating our migrations, let's create a new migration where we will store our posts.

Our posts table should have a few obvious columns that we will simplify for this tutorial part. Let's walk through how we create migrations with Masonite

Craft Command

Not surprisingly, we have a craft command to create migrations. You can read more about but we'll simplify it down to the command and explain a little bit of what's going on:

terminal
$ craft migration create_posts_table --create posts

This command simply creates the basis of a migration that will create the posts table. By convention, table names should be plural (and model names should be singular but more on this later).

This will create a migration in the databases/migrations folder. Let's open that up and starting on line 6 we should see look like:

databases/migrations/2018_01_09_043202_create_posts_table.py
def up(self):
    """
    Run the migrations.
    """
    with self.schema.create('posts') as table:
        table.increments('id')
        table.timestamps()

Lets add a title, an author, and a body to our posts tables.

databases/migrations/2018_01_09_043202_create_posts_table.py
def up(self):
    """
    Run the migrations.
    """
    with self.schema.create('posts') as table:
        table.increments('id')
        table.string('title')

        table.integer('author_id').unsigned()
        table.foreign('author_id').references('id').on('users')

        table.string('body')
        table.timestamps()

Now we can migrate this migration to create the posts table

terminal
$ craft migrate

This should be fairly straight forward but if you want to learn more, be sure to read the documentation.

Database Migrations here
Database Migrations