Artisan, creating tables in database
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Title: Creating Tables in Laravel Databases Using Artisan Commands
Artisan is a command-line interface (CLI) for the Laravel PHP framework that allows developers to perform various tasks such as creating and managing database tables, running automated tests, and generating code. In this comprehensive blog post, we'll explore how to use Artisan commands effectively in order to create MySQL tables in Laravel 5 using a simple example of a users table.
Firstly, let's walk through the process of setting up our project directory structure:
- Create a new Laravel application or open an existing one.
- Navigate to the project folder, which is typically located at
/path/to/project. - Within the project folder, create two directories named
databaseandmigrations. - Inside the
databasedirectory, create a file calledconnections.phpto define your database connection. - Ensure that you have created and populated the target MySQL database using whatever tools or interfaces are available to you. The database should have an existing schema with its tables in place.
Next, let's discuss how Artisan commands can be used to create and manage database tables:
Creating a table structurally in Laravel 5:
- Open your favorite code editor and create a new file called
filename.phpwithin the/project/database/migrationsdirectory. Replacefilenamewith an appropriate title for your table, such as "users" in this example. - Write the code to define the table structure in the file. For instance:php
public function up() { Schema::create('users', function(Blueprint $table) { $table->increments('id'); $table->string('username'); $table->string('fullname'); $table->int('number'); $table->string('email')->unique(); $table->string('password', 60); $table->rememberToken(); $table->timestamps(); }); }
This code snippet creates the users table with various fields, including an auto-incrementing primary key (id), a unique email field, and a string field for passwords. Note that some of the database types provided in this example may not be specific to MySQL; they are just examples to demonstrate the concept.
- Save the file and close your code editor.
- Open your favorite code editor and create a new file called
Now we're ready to run the migration using Artisan commands. Let's discuss each command and their purpose:
php artisan migrate: This command is used to execute all pending migrations in the specified database connection. If a new migration file is added, running this command will apply it to the database. In our case, as we haven't yet run any migration commands, this would create the users table based on its structure defined within themigrationsdirectory.php artisan migrate:install: This command is used for the first time to install the initial database migration setup. It generates a2014_10_12_00_00_initial.phpfile within the migrations directory, containing a default schema with only the migrations table and its structure.php artisan migrate --pretend: This command simulates running the migration without actually executing any changes to the database. It's useful for testing the migration code before applying it. If no errors are displayed, you can safely run the actual migration using themigratecommand.
It is essential to take precautions when working with databases, especially during development and testing phases. Always ensure that a copy of your database exists in case something goes wrong. Additionally, it's good practice to use a test database for running migrations and other tasks related to testing and debugging.
In conclusion, Artisan commands allow developers to build and manage database tables efficiently using Laravel 5. When creating table structures, consider best practices such as defining appropriate data types, enforcing unique constraints, and implementing appropriate indexes. Remember to use the correct Artisan commands for each task to ensure a smooth and safe development process. Don't forget to back up your databases regularly before making any major changes or running any migration commands.