Artisan, creating tables in database
Stefan Izdrail
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:
1. Create a new Laravel application or open an existing one.
2. Navigate to the project folder, which is typically located at
/path/to/project.
3. Within the project folder, create two directories named database and migrations.
4. Inside the database directory, create a file called connections.php to define your database connection.
5. 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:
1. Open your favorite code editor and create a new file called filename.php within the /project/database/migrations directory. Replace filename with an appropriate title for your table, such as "users" in this example.
2. 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.
3. Save the file and close your code editor.
Now we're ready to run the migration using Artisan commands. Let's discuss each command and their purpose:
1. 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 the migrations directory.
2. php artisan migrate:install: This command is used for the first time to install the initial database migration setup. It generates a 2014_10_12_00_00_initial.php file within the migrations directory, containing a default schema with only the migrations table and its structure.
3. 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 the migrate command.
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.