LogoLogo
12.1.0
12.1.0
  • Introduction
  • What's New?
  • Installation & Usage
  • Migration Guide
  • Contributing & Filing Issues
  • Query Builder
    • Getting a New Query
    • Building Queries
      • Selects
      • From
      • For
      • Joins
      • Wheres
      • Order By
      • Group By and Having
      • Limit, Offset, and Pagination
      • Locks
      • Unions
      • Common Table Expressions (i.e. CTEs)
      • Raw Expressions
      • When / Conditionals
      • Query Parameters and Bindings
    • Executing Queries
      • Retrieving Results
      • Aggregates
      • Inserts, Updates, and Deletes
    • Options and Utilities
      • Query Options and Utilities
      • Clone and Reset
      • Return Format
      • Column Formatter
      • Interception Points
    • Debugging
      • sqlCommenter
  • Schema Builder
    • Overview
    • Creating Tables and Views
    • Columns
    • Column Modifiers
    • Column Constraints
    • Creating Table Constraints
    • Altering Tables and Views
    • Dropping Tables and Views
    • Debugging
  • External Links
    • API Docs
    • Source Code
    • Issue Tracker
Powered by GitBook
On this page
  • create
  • createAs
  • createView

Was this helpful?

Edit on GitHub
Export as PDF
  1. Schema Builder

Creating Tables and Views

PreviousOverviewNextColumns

Was this helpful?

create

This method allows you to create a table object.

Argument

Type

Required

Default

Description

table

string

true

The name of the table to create.

callback

function

true

A callback function used to define the table body. It is passed a Blueprint as the only argument.

options

struct

false

{}

Options to pass to queryExecute.

execute

boolean

false

true

Run the query immediately after building it.

The majority of the work comes from calling methods on the Blueprint object. A Blueprint defines the and for your tables.

Example:

schema.create( "users", function( table ) {
    table.increments( "id" );
    table.string( "email" );
    table.string( "password" );
    table.timestamp( "created_date" );
    table.timestamp( "modified_date" );
    table.timestamp( "last_logged_in" ).nullable();
} );

This would convert to the following SQL in MySQL:

CREATE TABLE `users` (
    `id` INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `email` VARCHAR(255) NOT NULL,
    `password` VARCHAR(255) NOT NULL,
    `created_date` TIMESTAMP NOT NULL,
    `modified_date` TIMESTAMP NOT NULL,
    `last_logged_in` TIMESTAMP,
    CONSTRAINT `pk_users_id` PRIMARY KEY (`id`)
)

Only one table can be created at a time. If you wanted to create multiple tables, you would call create multiple times.

createAs

This method allows you to create a table using a query. It is similar to a view except that the data is inserted once at table creation.

This is an UnsupportedOperation on DerbyGrammar.

Argument
Type
Required
Default
Description

newTableName

string

true

The name of the table to create.

callback

function

true

A callback function used to define the table body. It is passed a QueryBuilder as the only argument.

options

struct

false

{}

Options to pass to queryExecute.

execute

boolean

false

true

Run the query immediately after building it.

Like with a view, the columns are defined by the data returned by the query. The data returned by the query will be inserted into the table.

createView

This method allows you to create a view using a query.

Argument
Type
Required
Default
Description

view

string

true

The name of the view to create.

callback

function

true

A callback function used to define the table body. It is passed a QueryBuilder as the only argument.

options

struct

false

{}

Options to pass to queryExecute.

execute

boolean

false

true

Run the query immediately after building it.

The callback argument is where you define the schema of your table. It is passed a Blueprint object. This is commonly aliased as table in the callback. Blueprint defines the field, index and constraint methods to build your table. You can find a comprehensive list of all available methods here for and here for .

columns
indexes
columns
indexes and constraints