Search…
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 columns and indexes for your tables.
Example:
1
schema.create( "users", function( table ) {
2
table.increments( "id" );
3
table.string( "email" );
4
table.string( "password" );
5
table.timestamp( "created_date" );
6
table.timestamp( "modified_date" );
7
table.timestamp( "last_logged_in" ).nullable();
8
} );
Copied!
This would convert to the following SQL in MySQL:
1
CREATE TABLE `users` (
2
`id` INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT,
3
`email` VARCHAR(255) NOT NULL,
4
`password` VARCHAR(255) NOT NULL,
5
`created_date` TIMESTAMP NOT NULL,
6
`modified_date` TIMESTAMP NOT NULL,
7
`last_logged_in` TIMESTAMP,
8
CONSTRAINT `pk_users_id` PRIMARY KEY (`id`)
9
)
Copied!
Only one table can be created at a time. If you wanted to create multiple tables, you would call create multiple times.
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 columns and here for indexes and constraints.
Last modified 3yr ago
Copy link
Edit on GitHub